A simple RESTful microservice built in Go using Fiber v2, GORM, and PostgreSQL.
This application follows the principles of Hexagonal Architecture (Ports and Adapters). The goal is to isolate the business logic from infrastructure, enabling better testability, maintainability, and flexibility.
- Domain Layer: Core business models and logic (
internal/domain). - Application Layer: Use cases and interfaces/ports (
internal/application). - Adapters Layer: Infrastructure implementations - HTTP handlers, database repositories (
internal/adapter). - Configuration Layer: Centralized configuration management (
internal/config). - Entrypoint: Application initialization and wiring (
cmd/server).
This separation keeps the core application logic independent from frameworks, databases, and external systems.
This project implements the following task: View assignment
POST /save: Store user data in the database.GET /:id: Retrieve user data byexternal_id.
API documentation is available in OpenAPI 3.0 format: OpenAPI Specification
GET /healthzResponse:
{"status": "ok"}POST /save
Content-Type: application/json
{
"external_id": "550e8400-e29b-41d4-a716-446655440000",
"name": "John Doe",
"email": "john.doe@example.com",
"date_of_birth": "1990-01-15T00:00:00Z"
}Success Response (201):
{
"id": 1,
"external_id": "550e8400-e29b-41d4-a716-446655440000",
"name": "John Doe",
"email": "john.doe@example.com",
"date_of_birth": "1990-01-15T00:00:00Z"
}Error Responses:
400 Bad Request: Invalid input or validation error409 Conflict: User with this external_id already exists429 Too Many Requests: Rate limit exceeded500 Internal Server Error: Server error
GET /{external_id}Success Response (200): Same as Create User response
Error Responses:
404 Not Found: User not found429 Too Many Requests: Rate limit exceeded500 Internal Server Error: Server error
- Go 1.24+
- Docker
- Docker Compose
make(optional but recommended)
git clone https://github.com/podanypepa/wbrestapi.git
cd wbrestapiCopy .env.localhost and modify if needed:
cp .env.localhost .envEnvironment config examples:
- .env.localhost - For local development
- .env.docker - For Docker Compose
make docker-upThis will start the app and PostgreSQL database, initialize schema, and make the app available at
http://localhost:3000.
make test
# or
go test -v ./...Integration tests start a real server and test HTTP endpoints:
cd cmd/server
go test -vNote: Integration tests require PostgreSQL running on localhost:5432
make build
make run
# or run in dev/watch mode
make devmake docker-downTo reset DB data:
rm -rf ./data- create_user.sh: Create a new user from the command line
- get_user.sh: Retrieve users by UUID from the command line
- β¨ JSON REST API with Go + Fiber
- π PostgreSQL + GORM
- π Input validation with structured errors
- π¦ Rate limiting (100 req/min by default)
- π Structured logging (JSON format)
- π‘οΈ Panic recovery middleware
- π₯ Health check endpoint
- β‘ Graceful shutdown (SIGINT/SIGTERM)
- π³ Dockerized and portable
- β Comprehensive test coverage (unit + integration)
- π Database connection pooling
- π CI/CD with GitHub Actions
- π OpenAPI 3.0 documentation
- π― Hexagonal Architecture
Configure via environment variables:
PORT- Server port (default: 3000)SHUTDOWN_TIMEOUT- Graceful shutdown timeout (default: 5s)RATE_LIMIT_MAX- Max requests per window (default: 100)RATE_LIMIT_WINDOW- Rate limit time window (default: 1m)LOG_LEVEL- Logging level: info, debug (default: info)
DB_HOST- Database hostDB_USER- Database userDB_PASSWORD- Database passwordDB_NAME- Database nameDB_PORT- Database portDB_SSL- SSL mode (disable, require, etc.)DB_MAX_OPEN_CONNS- Max open connections (default: 25)DB_MAX_IDLE_CONNS- Max idle connections (default: 5)DB_CONN_MAX_LIFETIME- Connection max lifetime (default: 5m)
wbrestapi/
βββ api/ # API documentation
β βββ openapi.yaml # OpenAPI 3.0 specification
βββ cmd/
β βββ server/ # Application entrypoint
β βββ main.go
β βββ integration_test.go
βββ internal/
β βββ adapter/ # Infrastructure adapters
β β βββ handler/ # HTTP handlers
β β βββ repository/ # Database repositories
β βββ application/ # Application layer
β β βββ port/ # Interfaces (ports)
β β βββ usecase/ # Use cases
β βββ config/ # Configuration
β βββ domain/ # Domain models and logic
βββ .github/
β βββ workflows/ # CI/CD pipelines
βββ Dockerfile
βββ compose.yaml
βββ Makefile
βββ README.md
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.