go-customer-api is a Go REST API for basic customer management.
It uses Gorilla Mux for routing, PostgreSQL for persistence, and Gormigrate to run schema/data migrations automatically on startup.
- CRUD endpoints for customers
- Request validation for customer IDs and payload fields
- Auto-migration and seed data at application startup
- Swagger/OpenAPI documentation
- Go
- Gorilla Mux
- PostgreSQL
- GORM + Gormigrate
- Swaggo (
swaggerUI)
controllers/ HTTP handlers
db/ DB connection and runtime migrations
docs/ Generated Swagger files
models/ Data models
routes/ Route registration and middleware
script/ Local DB bootstrap scripts
main.go Application entrypoint
- Go 1.24+
- PostgreSQL (local install or Docker)
Copy .env.example to .env and fill values:
DB_USER=yourusername
DB_PASSWORD=yourpassword
DB_NAME=yourdbname
DB_HOST=localhost
DB_PORT=5432The app requires all five variables.
Use either option:
docker compose up -d postgres./script/create_postgres_db.sh
The script can read values from .env and creates the customers table if missing.
go mod tidy
go run main.goServer URL: http://localhost:8000
On startup, the app:
- Connects to PostgreSQL
- Runs Gormigrate migrations in
db/db.go - Seeds sample customers if they do not already exist
GET /api/customers- list all customersGET /api/customers/{id}- get one customerPOST /api/customers- create customerPUT /api/customers/{id}- update customerDELETE /api/customers/{id}- delete customer
Customer payload:
{
"first_name": "John",
"last_name": "Doe",
"email": "john.doe@example.com"
}After server start:
http://localhost:8000/swagger/index.html
Regenerate docs after changing handler annotations:
swag initInstall swag if needed:
go install github.com/swaggo/swag/cmd/swag@latest