A Go-based web service that exposes user-related endpoints (status, leaderboard, task completion, referral assignment) backed by PostgreSQL. The project uses Gin for HTTP routing and pgx for database access.
- Retrieve user status with balance and profile information.
- View a leaderboard of users sorted by balance with optional limit/offset.
- Mark user tasks as completed and return updated task metadata.
- Assign a referrer to a user while enforcing validation rules (self-referral, duplicates, missing users).
cmd/api # Application entry point
internal/adapter # HTTP handlers, routing, and PostgreSQL repository implementation
internal/core # Domain models, use cases, and repository ports
migrations # SQL migrations for users/tasks tables and seed data
- Go 1.24+
- Docker and Docker Compose (optional but recommended)
- PostgreSQL (Docker setup provided)
- golang-migrate CLI if running migrations locally
Create a .env file (sample used by Docker Compose):
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=simple_http_server
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
APP_DB_DSN=postgres://postgres:postgres@postgres:5432/simple_http_server?sslmode=disable
Adjust credentials/hostnames as needed for your environment.
docker compose up --buildThis starts:
postgres– PostgreSQL 17 with persistence viapgdatavolume.migrate– Applies SQL migrations on startup.app– Gin server compiled with Delve debugger support (ports 8080 for HTTP, 40000 for DAP).
The API becomes available at http://localhost:8080 once migrations finish and the app starts.
- Ensure PostgreSQL is running and migrations are applied via
migrateCLI:migrate -path migrations -database "$APP_DB_DSN" up - Export
APP_DB_DSNto match your database. - Run the server:
go run ./cmd/api
GET /users/:id/status– Fetch user profile and balance.GET /users/leaderboard?limit=&offset=– List users sorted by balance.POST /users/:id/task/complete– Body{ "task_id": number }; marks the task as done.POST /users/:id/referrer– Body{ "referrer_id": number }; assigns a referrer.
Responses follow JSON structures defined in internal/core/usecase DTOs. Errors are mapped to HTTP status codes via internal/adapter/http/user_handler.go.