Minimal Go HTTP API starter with Chi, PostgreSQL, Atlas migrations, and sqlc code generation.
This project is a lightweight API server built with Go 1.26, designed to be simple yet robust. Key features include:
- Router:
chiv5 for fast and composable routing. - Database:
pgxv5 for high-performance PostgreSQL driver and connection pooling. - Migrations: Managed by Atlas for declarative schema management (
internal/adapters/migrations/schema.hcl). - Code Generation: sqlc for type-safe SQL queries (
internal/adapters/sqlc). - Logging: Structured logging using Go's standard library
slog. - Task Runner:
Taskfile.ymlfor simplified command execution.
.
├── cmd/ # Application entrypoints
│ ├── main.go # Service entrypoint
│ └── api.go # HTTP server and router setup
├── internal/
│ ├── adapters/ # Interface adapters (database, migrations)
│ │ ├── migrations/ # Atlas schema definition (schema.hcl) and SQL dump (schema.sql)
│ │ └── sqlc/ # sqlc generated Go code and SQL queries
│ ├── env/ # Environment variable utilities
│ ├── json/ # JSON response helpers
│ ├── orders/ # Domain logic for order management
│ └── products/ # Domain logic for product management
├── atlas.hcl # Atlas configuration file
├── sqlc.yml # sqlc configuration file
└── Taskfile.yml # Task runner definitionsEnsure you have the following installed:
- Go: 1.26 or higher
- PostgreSQL: 16 or higher (or Docker)
- Atlas CLI: For managing database schemas
- MacOS:
brew install ariga/tap/atlas - Windows/Linux: Download from ariga.io
- MacOS:
- Task: (Optional) For running
Taskfile.ymlcommands. Install via taskfile.dev. - sqlc: (Optional) For regenerating database code. Install via sqlc.dev.
-
Clone the repository
git clone https://github.com/DDRMin/GO-Backend.git cd GO-Backend -
Environment Setup
Configure your database connection string. You can set the
DB_URLenvironment variable or use a.envfile if you implement one.Windows (PowerShell):
$env:DB_URL="postgres://user:password@localhost:5432/mydb?sslmode=disable"
Mac/Linux:
export DB_URL="postgres://user:password@localhost:5432/mydb?sslmode=disable"
-
Install Dependencies
go mod tidy
-
Database Setup (Atlas)
Apply the database schema defined in
internal/adapters/migrations/schema.hcl.# Using Taskfile (Recommended) task atlas-apply # Manual command atlas schema apply --env local
This command reads
DB_URL, compares the declarative schema with the database state, and applies necessary changes. -
Run the Application
go run ./cmd
The server will start on port
8080(default).
- Modify
internal/adapters/migrations/schema.hcl. - Apply changes to your local database:
This also updates
task atlas-apply
internal/adapters/migrations/schema.sqlwhich serves as the source of truth forsqlc.
If you add or modify SQL queries in internal/adapters/sqlc/queries.sql:
- Run the generator:
task sqlc # OR sqlc generate - Use the generated methods in your Go code (see
internal/products/service.gofor examples).
| Method | Endpoint | Description |
|---|---|---|
GET |
/health |
Health check. Returns 200 OK or 503 Unavailable. |
GET |
/products |
Retrieves a list of products. |
GET |
/products/{id} |
Retrieves a single product by ID. |
POST |
/products |
Creates a new product. |
POST |
/orders |
Creates a new order with line items. |
The application is configured via environment variables:
| Variable | Description | Default |
|---|---|---|
DB_URL |
PostgreSQL connection string | (Required) |
PORT |
Port for the HTTP server (if configured in config.go) |
127.0.0.1:8080 |