Go + Gin service for ingesting events into Postgres with API key auth, structured logging, and basic querying.
- POST /events to ingest validated events (source, event_type, timestamp, payload string)
- GET /events with filtering, sorting, and pagination
- PostgreSQL storage with auto-migration and indexes on type/source/timestamp
- API key authentication (Bearer) and structured zap logging with panic recovery
- Docker/Docker Compose for local Postgres + app
- Entrypoint: cmd/main.go wires config, Postgres, middlewares, and routes
- HTTP: Gin with custom request logging and panic recovery
- Storage: GORM Postgres driver, auto-migrate models.Event, indexes created in storage.InitPostgres
- Config: env-driven (APP_PORT, APP_ENV, DB_*, API_KEY)
- Error responses: centralized JSON shape via internal/utils/errors.go
- Copy envs:
cp .env.example .envand set DB creds/API_KEY as needed. - Start Postgres via Docker Compose:
docker-compose up -d postgres. - Run the API:
go run ./cmd/main.go. - Hit health:
curl http://localhost:8080/health.
cp .env.example .env
docker-compose up --build
# API defaults to :8080 unless APP_PORT is setIf API_KEY is set, send Authorization: Bearer <API_KEY>.
curl -X POST http://localhost:8080/events \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{
"source": "web",
"event_type": "page_view",
"timestamp": "2024-01-01T00:00:00Z",
"payload": "{\\"path\\":\\"/home\\"}"
}'Query params: type, source, sort (asc|desc), limit, offset.
curl -X GET "http://localhost:8080/events?type=page_view&source=web&limit=50" \
-H "Authorization: Bearer $API_KEY"- APP_PORT (default 8080)
- APP_ENV (development|production) controls zap config
- DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME, DB_SSLMODE
- API_KEY (optional; if empty, endpoints are open)
- .env is loaded automatically on startup for local development
APP_ENV=production-> zap production config (JSON, no colors)- other -> zap development config (color levels)
- Request logging + panic recovery middlewares are registered in cmd/main.go
- Auto-migration runs on start for models.Event
- Indexes on event_type, source, timestamp created in storage.InitPostgres
go test ./...- cmd/main.go — bootstrap server
- internal/config — env config loader
- internal/api — routes, middleware
- internal/models — data models
- internal/storage — Postgres wiring and helpers
- internal/services — service-level helpers
- internal/utils — error and logging utilities