GoServerTemplate

Production-ready Go server template with layered architecture, dependency injection, and PostgreSQL support.
Architecture
cmd/server → Application entrypoint (FX bootstrap)
internal/config → Environment-based configuration
internal/db → PostgreSQL connection (sqlx + pgx)
internal/handler → HTTP handlers (handler → service → repo)
internal/service → Business logic
internal/repository → Data access layer
internal/router → gorilla/mux routing + middleware
pkg/logger → Structured logging (slog)
Dependencies
Environment Variables
| Variable |
Default |
Description |
PORT |
:8080 |
Server listen address |
DB_DSN |
"" |
Postgres DSN (optional) |
LOG_LEVEL |
info |
slog level: debug/info/warn/error |
Running Locally
# Run server
go run ./cmd/server
# Run tests
go test ./...
# Build binary
go build -o server ./cmd/server
./server
Docker
docker build -t go-server-template .
docker run -p 8080:8080 -e PORT=:8080 go-server-template
Routes
| Method |
Path |
Description |
| GET |
/ |
Welcome / status |
| GET |
/health |
Health check (DB ping) |