Skip to content

A comprehensive starter kit for building RESTful APIs in Go with PostgreSQL as the database backend. This starter kit includes Swagger for API documentation, default authentication setup, dynamic tooling for database migrations and CRUD API generation, cache middleware for performance optimization, and goroutines for concurrent execution.

Notifications You must be signed in to change notification settings

JubaerHossain/go-starter-with-rootx

Repository files navigation

   ___  ____  ____  _______  __
  / _ \/ __ \/ __ \/_  __/ |/_/
 / , _/ /_/ / /_/ / / / _>  <  
/_/|_|\____/\____/ /_/ /_/|_|  
                               

🚀 Go Starter with Postgres + Go + Swagger Default Auth + REST API + Cache Middleware + Goroutines 🚀

A comprehensive starter kit for building RESTful APIs in Go with PostgreSQL as the database backend. This starter kit includes Swagger for API documentation, default authentication setup, dynamic tooling for database migrations and CRUD API generation, cache middleware for performance optimization, and goroutines for concurrent execution. Kickstart your Go project with ease and efficiency!

Features:

  • PostgreSQL database integration
  • RESTful API endpoints
  • Swagger documentation
  • Default authentication setup
  • Dynamic tooling for database migrations and CRUD API generation
  • Cache middleware for performance optimization
  • Goroutines for concurrent execution

Getting Started

Prerequisites

  • Go 1.16 or higher
  • Docker
  • Docker Compose
  • Node.js

Installation

  1. Clone the repository:

    git clone https://github.com/JubaerHossain/rootx.git
    cd rootx
  2. Build and run the application using Docker Compose:

    docker-compose up --build
  3. Access the application at http://localhost:8080.

Usage

  • To start the server:

    go run cmd/main.go
  • To run tests:

    go test ./...

install dependencies

make install

run the project [development mode]

make dev
Select a command:
1. Create Migration
2. Create Migration with Seeder
3. Apply Migrations
4. Run Seeders
Enter the command number: 4
Running seeders...
2024/06/04 19:35:32 seeds files executed successfully
Task completed successfully

create a new migration example [select 1]

make rootx 

run the migration [select 2]

make rootx

seed the database [select 3]

make rootx

create a new module

make command name=module_name

build the project

make build

run the project [production mode]

make run

uses database postgres

sudo -u postgres psql postgres

Code check by golangci-lint linting and formatting code

make lint

docker volume list

docker volume ls

docker volume remove

docker volume rm <volume_name>

docker shell

docker exec -it <container_id> /bin/sh

or

docker exec -it <container_id> bash

docker postgres

docker run --name postgres -e POSTGRES_PASSWORD=postgres -d -p 5432:5432 postgres

docker postgres with volume

docker run --name postgres -e POSTGRES_PASSWORD=postgres -d -p 5432:5432 -v postgres:/var/lib/postgresql/data postgres

docker postgres with volume and password

docker run --name postgres -e POSTGRES_PASSWORD=postgres -d -p 5432:5432 -v postgres:/var/lib/postgresql/data postgres

docker postgres with volume and password and user

docker run --name postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_USER=postgres -d -p 5432:5432 -v postgres:/var/lib/postgresql/data postgres

docker postgres with volume and password and user and database

docker run --name postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_USER=postgres -e POSTGRES_DB=restaurant-api -d -p 5432:5432 -v postgres:/var/lib/postgresql/data postgres

postgres shell

psql -h localhost -U postgres

Directory Structure

project
│
.
├── cmd
│   └── main.go
├── config
│   └── config.go
├── internal
│   ├── app.go
│   ├── domain
│   │   ├── comment
│   │   │   ├── entity.go
│   │   │   ├── repository.go
│   │   │   └── service.go
│   │   ├── post
│   │   │   ├── entity.go
│   │   │   ├── repository.go
│   │   │   └── service.go
│   │   ├── user
│   │   │   ├── entity.go
│   │   │   ├── repository.go
│   │   │   └── service.go
│   ├── interfaces
│   │   ├── grpc
│   │   │   ├── proto
│   │   │   │   └── service.proto
│   │   │   └── server.go
│   │   ├── http
│   │   │   ├── handler.go
│   │   │   ├── router.go
│   │   │   └── server.go
│   ├── usecase
│   │   ├── comment.go
│   │   ├── post.go
│   │   └── user.go
│   └── templates
│       ├── layouts
│       │   └── base.html
│       ├── partials
│       │   ├── footer.html
│       │   ├── header.html
│       │   └── sidebar.html
│       └── views
│           ├── auth
│           │   ├── login.html
│           │   └── register.html
│           ├── cart
│           │   └── index.html
│           ├── products
│           │   ├── detail.html
│           │   └── list.html
│           └── home.html
├── pkg
│   ├── auth
│   │   └── jwt.go
│   ├── cache
│   │   └── redis.go
│   ├── db
│   │   └── postgres.go
│   ├── logger
│   │   └── logger.go
│   ├── middleware
│   │   └── auth.go
│   ├── response
│   │   └── response.go
│   ├── tasks
│   │   └── background.go
│   └── workerpool
│       └── workerpool.go
├── static
│   ├── css
│   │   └── tailwind.css
│   ├── js
│   │   └── main.js
│   └── images
└── go.mod

Description

  • cmd: Entry point of the application.

    • main.go: Main executable file.
  • config: Configuration settings.

    • config.go: Configuration management.
  • pkg: External packages.

    • cache: Caching mechanisms.
      • redis.go: Redis cache implementation.
    • db: Database connections.
      • postgres.go: PostgreSQL database implementation.
    • logger: Logging utilities.
      • logger.go: Logger setup.
    • middleware: HTTP middleware.
      • auth.go: Authentication middleware.
      • cors.go: CORS handling.
      • logging.go: Request logging.
      • ratelimit.go: Rate limiting.
    • auth: Authentication mechanisms.
      • jwt.go: JWT handling.
    • response: HTTP response utilities.
      • response.go: Response formatting.
    • tasks: Background tasks.
      • background.go: Background task management.
    • workerpool: Worker pool for concurrent task processing.
      • workerpool.go: Worker pool implementation.
  • internal: Internal application logic.

    • domain: Domain logic for entities.
      • user: User domain.
        • entity.go: User entity definition.
        • repository.go: User repository interface.
        • service.go: User service implementation.
      • post: Post domain.
        • entity.go: Post entity definition.
        • repository.go: Post repository interface.
        • service.go: Post service implementation.
      • comment: Comment domain.
        • entity.go: Comment entity definition.
        • repository.go: Comment repository interface.
        • service.go: Comment service implementation.
    • interfaces: External interfaces (e.g., HTTP, gRPC).
      • grpc: gRPC server and proto definitions.
        • server.go: gRPC server implementation.
        • proto: Protocol buffer definitions.
          • service.proto: gRPC service definitions.
      • http: HTTP server.
        • handler.go: HTTP handlers.
        • router.go: HTTP routing.
        • server.go: HTTP server setup.
    • usecase: Application use cases.
      • user.go: User use cases.
      • post.go: Post use cases.
      • comment.go: Comment use cases.
    • app.go: Application setup and initialization.
  • html: Frontend templates and static files.

    • index.html: Homepage template.
    • login.html: Login page template.
    • posts.html: Posts listing page template.
    • post.html: Single post page template.
    • styles.css: CSS styles.
    • htmx.min.js: HTMX library.
  • Dockerfile: Docker configuration for containerizing the application.

  • docker-compose.yml: Docker Compose configuration for multi-container applications.

  • go.mod: Go module file.

  • go.sum: Go dependencies.

  • package.json: Node.js package configuration.

  • postcss.config.js: PostCSS configuration.

  • tailwind.config.js: Tailwind CSS configuration.

Features

  • Golang
  • DDD
  • Clean Architecture
  • Docker
  • Makefile
  • Swagger
  • Gorm
  • JWT
  • Viper
  • Logger[Zap]
  • Unit Test

Contributing

  1. Fork the repository.
  2. Create a new feature branch (git checkout -b feature-branch).
  3. Commit your changes (git commit -m 'Add some feature').
  4. Push to the branch (git push origin feature-branch).
  5. Open a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements


Copy and paste the above markdown text into your README.md file on GitHub. This file provides an organized overview of your project, including the directory structure, descriptions of each component, installation instructions, usage guidelines, contribution steps, and acknowledgements.

About

A comprehensive starter kit for building RESTful APIs in Go with PostgreSQL as the database backend. This starter kit includes Swagger for API documentation, default authentication setup, dynamic tooling for database migrations and CRUD API generation, cache middleware for performance optimization, and goroutines for concurrent execution.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published