A production-ready REST API template built with Gin, GORM, and supporting multiple database backends. This project follows Domain-Driven Design (DDD) principles and provides a solid foundation for building scalable Go web applications.
- Fast HTTP Framework: Built with Gin for high performance
- Multiple Database Support: PostgreSQL, MySQL, and SQLite through GORM
- Clean Architecture: Follows DDD principles with clear separation of concerns
- Configuration Management: YAML-based configuration with environment support
- Middleware Support: Built-in CORS and logging middleware
- CLI Interface: Powered by Cobra for command-line operations
- Docker Ready: Includes Dockerfile for containerization
├── cmd/ # Command line interface
│ └── root.go # Root command and application bootstrap
├── internal/ # Private application code
│ ├── common/ # Shared utilities and middleware
│ │ ├── errors/ # Error handling utilities
│ │ └── middleware/ # HTTP middleware (CORS, logging)
│ └── config/ # Configuration management
├── pkg/ # Public packages
│ └── database/ # Database abstraction layer
├── config.yaml # Application configuration
├── Dockerfile # Container configuration
├── Makefile # Build automation
└── main.go # Application entry point
- Go 1.23.6 or higher
- PostgreSQL, MySQL, or SQLite (depending on your choice)
- Make (optional, for using Makefile commands)
-
Clone the repository:
git clone https://github.com/shuv1824/go-api-starter.git cd go-api-starter -
Install dependencies:
go mod download
-
Configure the application:
cp config.yaml.example config.yaml # if example exists # Edit config.yaml with your database settings
The application uses a YAML configuration file (config.yaml). Here's the default structure:
mode: debug # Application mode: debug, test, release
port: 8080 # Server port
database:
type: postgres # Database type: postgres, mysql, sqlite
host: localhost
port: 5432
username: postgres
password: 123456
dbname: gostarter
sslmode: disable# Build and run
make
# Build only
make build# Run directly
go run main.go
# Build and run binary
go build -o bin/apiserver .
./bin/apiserverThe application includes a health check endpoint:
GET /ping- Returns a simple pong response
The application supports multiple database backends through a factory pattern:
- PostgreSQL (default)
- MySQL
- SQLite
Simply change the database.type in your configuration file to switch between databases.
This project follows the Standard Go Project Layout:
cmd/- Main applications for this projectinternal/- Private application and library codepkg/- Library code that's safe to use by external applications
- Domain Logic: Add business logic in appropriate packages under
internal/ - Database Models: Define GORM models and repositories in domain packages
- HTTP Handlers: Create REST endpoints in handler packages
- Middleware: Add custom middleware in
internal/common/middleware/
The application includes several built-in middleware:
- CORS: Cross-origin resource sharing support
- Logging: Request/response logging
- Recovery: Panic recovery middleware
Build and run with Docker:
# Build Docker image
docker build -t go-api-starter .
# Run container
docker run -p 8080:8080 go-api-starter# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run tests with verbose output
go test -v ./...