A RESTful API built in Go for managing users with name and date of birth. Age is calculated dynamically on read operations.
- Full CRUD operations for users
- Dynamic age calculation based on date of birth
- Pagination support for listing users
- Input validation with custom date format checking
- Structured logging with request ID and duration tracking
- Type-safe database access using SQLC
- Containerized with Docker Compose
- Go with Fiber framework
- PostgreSQL
- SQLC for query generation
- go-playground/validator for validation
- Uber Zap for logging
- pgx PostgreSQL driver
/cmd/server # Application entry point
/db/migrations # Database schema migrations
/db/query # SQLC query definitions
/db/sqlc # Generated SQLC code
/internal
├── dto # Request/response DTOs
├── handler # HTTP handlers
├── middleware # Request ID and logging middleware
├── models # Application models
├── repository # Data access layer
├── service # Business logic
├── logger # Zap logger configuration
└── db # Database connection
# Build and start the services
docker compose up --build
# Subsequent runs
docker compose upThe API will be available at http://localhost:3000
PostgreSQL will be available at localhost:5432
- Start PostgreSQL (e.g., via Docker):
docker run --name user-api-postgres \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_USER=postgres \
-e POSTGRES_DB=userdb \
-p 5432:5432 \
-v pgdata:/var/lib/postgresql/data \
-d postgres:15- Apply the migration:
docker exec -i user-api-postgres psql -U postgres -d userdb < db/migrations/001_create_users_table.sql- Run the application:
go run cmd/server/main.go| Method | Endpoint | Description |
|---|---|---|
| POST | /users | Create a user |
| GET | /users | List users (with pagination) |
| GET | /users/:id | Get a user by ID |
| PUT | /users/:id | Update a user |
| DELETE | /users/:id | Delete a user |
GET /users supports:
limit— number of results per page (default: 20, max: 100)offset— number of results to skip (default: 0)
Example: /users?limit=10&offset=20
# Create a user
curl -X POST http://localhost:3000/users \
-H "Content-Type: application/json" \
-d '{"name": "Parth", "dob": "2004-05-10"}'
# Get a user by ID
curl http://localhost:3000/users/1
# List users (with pagination)
curl "http://localhost:3000/users?limit=5&offset=0"
# Update a user
curl -X PUT http://localhost:3000/users/1 \
-H "Content-Type: application/json" \
-d '{"name": "Parth Kalani", "dob": "2004-08-10"}'
# Delete a user
curl -X DELETE http://localhost:3000/users/1- Each request includes an
X-Request-IDresponse header - Logs contain request method, path, status, duration, and request ID