A comprehensive template for building RESTful APIs in Go. This template includes everything you need to get started with building a production-ready API.
- Routing: Using Chi for flexible and fast HTTP routing
- Authentication: JWT-based authentication with middleware
- Database: PostgreSQL integration with connection pooling
- Migrations: Database migrations using golang-migrate
- CRUD Operations: Example CRUD operations for a User entity
- Validation: Request validation using validator
- Documentation: API documentation using Swagger/OpenAPI
- Configuration: Environment-based configuration with sensible defaults
- Logging: Structured logging
- Error Handling: Consistent error handling and responses
- Middleware: Common middleware for request ID, real IP, logging, recovery, timeouts, and CORS
- Graceful Shutdown: Graceful shutdown of the HTTP server
.
├── api/
│ └── swagger/ # Swagger/OpenAPI specifications
├── cmd/
│ └── api/ # Application entry points
│ └── main.go # Main application
├── docs/ # Documentation
├── internal/ # Private application code
│ ├── config/ # Configuration package
│ ├── database/ # Database connection and migrations
│ ├── handlers/ # HTTP handlers
│ ├── middleware/ # HTTP middleware
│ ├── models/ # Data models
│ ├── repository/ # Data access layer
│ └── services/ # Business logic
├── migrations/ # Database migrations
├── pkg/ # Public packages
│ ├── logger/ # Logging package
│ └── validator/ # Validation package
- Go 1.21 or higher
- PostgreSQL
- Docker (optional)
- Clone the repository:
git clone https://github.com/Romasmi/go-rest-api-template.git
cd go-rest-api-template- Install dependencies:
go mod download- Create a
override.yamlfile (default values are in config.yaml) or set ENV variables:
cp config.yaml override.yaml- Start PostgreSQL:
# Using Docker
docker run --name postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_USER=postgres -e POSTGRES_DB=go_rest_api -p 5432:5432 -d postgres- Run database migrations:
go run cmd/api/main.go migrate- Start the API:
go run cmd/api/main.go-
Access the API at http://localhost:8080
-
Access the Swagger documentation at http://localhost:8080/swagger/
POST /api/v1/auth/register- Register a new userPOST /api/v1/auth/login- Login a user
GET /api/v1/users- List users (requires authentication)GET /api/v1/users/{id}- Get a user by ID (requires authentication)PUT /api/v1/users/{id}- Update a user (requires authentication)DELETE /api/v1/users/{id}- Delete a user (requires authentication)
GET /health- Health check endpoint
The application can be configured using environment variables. See the .env.example file for available options.
- Create a new model in
internal/models/ - Create a new repository in
internal/repository/ - Create a new service in
internal/services/ - Create a new handler in
internal/handlers/ - Register the new handler in
cmd/api/main.go
- Create a new migration file in
migrations/:
# Create a migration to add a new table
touch migrations/000002_create_items_table.up.sql
touch migrations/000002_create_items_table.down.sql- Run the migration:
go run cmd/api/main.go migrate