A production-level, highly scalable Go backend built using the Echo v4 HTTP framework. It utilizes connection pooled PostgreSQL via pgx/v5 and compiled type-safe database queries via SQLC, backed by Redis and structured logging via Zap.
- HTTP Framework: Echo v4
- Database Client: pgx/v5 with connection pooling (
pgxpool) - Query Generator: SQLC for compilation-time type-safe database methods
- Database Migrations: Goose
- Configuration Parsing: caarlos0/env/v11 with
.envloading usinggodotenv - Structured Logging: Uber Zap
- Caching: go-redis/v9
- Validation: go-playground/validator/v10
- Hot-Reloading: Air
- API UI Documentation: Scalar API Reference
├── cmd/
│ ├── api/ # HTTP server entrypoint & graceful shutdown
│ ├── worker/ # Asynq job consumer entrypoint
│ └── migrate/ # Goose migration CLI entrypoint
├── internal/
│ ├── controllers/ # HTTP handlers
│ ├── db/ # SQLC generated code
│ ├── dto/ # Request/Response structures & validation
│ ├── logging/ # Structured logging using Zap
│ ├── middlewares/ # Custom middlewares (JWT, Custom logger)
│ ├── queue/ # Asynq client (producer side)
│ ├── workers/ # Asynq task handlers (consumer side)
│ └── helpers/utils/ # DB Pool, Redis, config, validator
├── database/
│ ├── queries/ # SQLC raw queries
│ └── schema/ # Goose SQL migrations
├── docs/ # Documentation mirror explaining files
└── tests/ # HTTP and unit test cases
- Make sure you have PostgreSQL and Redis running locally.
- Create your
.envfile from the example:cp .env.example .env
- Initialize the database schema using Goose:
make migrate-up
- Run the application with hot-reloading:
make dev
- In a separate shell, run the worker:
make worker
There are two Docker Compose stacks, both defined by the same docker-compose.yml
and a docker-compose.dev.yml overlay:
Production-mode stack (Dockerfile, compiled binaries, one-shot migration job):
make docker-up # build + start postgres, redis, migrate, api, worker
make docker-logs # tail logs
make docker-down # stopThe migrate service runs goose up once and exits; api and worker wait for
it to complete successfully before starting.
Hot-reload dev stack (Dockerfile.dev, air, bind-mounted source):
make dev-up # build + start the same stack with live reload
make dev-logs
make dev-down-
CORS and cookies:
FRONTEND_URLandADMIN_URLmust be the exact origins of the portal and admin panel (scheme, host, no path); only those origins can call the API. Keep both on acodechefvit.comsubdomain so the session cookies are same-site;COOKIE_DOMAINcan stay unset. -
Reverse proxy:
GET /result/:idholds the request for up to 90s while a verdict is judged, and/runcode//runcustomfor up to 45s. Nginx's defaultproxy_read_timeout(60s) cuts/resultshort, so set it to at least 100s and turn buffering off for it:location /result/ { proxy_pass http://127.0.0.1:8080; proxy_read_timeout 100s; proxy_buffering off; }
Forward the client address (
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;); behind Cloudflare the API usesCF-Connecting-IP. -
Tuning:
RATE_LIMIT_MAX/RATE_LIMIT_WINDOW(per player, all routes),JUDGE0_WAIT_SLOTS(concurrent synchronous Judge0 calls; size it to the Judge0 worker count),WORKER_COUNTandPOSTGRES_MAX_CONNS. See.env.example.
Once the backend is running, navigate to:
http://localhost:8080/docs
to view the interactive API reference served via Scalar.
make build: Compile all binaries (api,worker,migrate) intobin/.make run: Run the API server locally.make worker: Run the worker locally.make dev: Run the API with hot-reloading (air).make test: Run automated tests.make lint: Run code formatting and static analysis checkers (golangci-lint).make vulncheck: Scan dependencies for known security vulnerabilities (govulncheck).make sqlc: Compile SQL queries into Go code using SQLC.make migrate-up/make migrate-down/make migrate-status: Manage Goose database migrations.make docker-up/make docker-down/make docker-logs: Manage the production-mode Docker stack (postgres, redis, migrate, api, worker).make docker-migrate: Run the one-shot migration job against the Docker stack.make dev-up/make dev-down/make dev-logs: Manage the hot-reload Docker dev stack.make clean: Remove build artifacts (bin/,tmp/).make help: List all available targets.
To enforce professional commit names (following the Conventional Commits spec), run the following command to bind the git hooks path:
git config core.hooksPath .githooks(On Linux/macOS, ensure the hook is executable: chmod +x .githooks/commit-msg)
Every Pull Request and commit pushed to main/master/dev triggers a GitHub Actions pipeline validating:
- Proper Go code formatting (
gofmt). - Static analysis checks (
go vet). - Code styling & security linters (
golangci-lint). - Automated tests suite completion (
go test). - Go application compilation health (
go build).
- Participant Portal:
cookoff-portal-11.0 - Admin Portal:
cookoff-admin-11.0
These services work together to provide the complete Cookoff 11.0 contest infrastructure.
Distributed under the MIT License. See LICENSE for more information.
Made with ❤️ by CodeChef-VIT