Skip to content

Rediet404/Event-Ingestion-Backend

Repository files navigation

Event Ingestion Backend

Go + Gin service for ingesting events into Postgres with API key auth, structured logging, and basic querying.

Features

  • 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

Architecture

  • 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

Quickstart (local)

  1. Copy envs: cp .env.example .env and set DB creds/API_KEY as needed.
  2. Start Postgres via Docker Compose: docker-compose up -d postgres.
  3. Run the API: go run ./cmd/main.go.
  4. Hit health: curl http://localhost:8080/health.

Quickstart (Docker Compose app + db)

cp .env.example .env
docker-compose up --build
# API defaults to :8080 unless APP_PORT is set

API

Auth

If API_KEY is set, send Authorization: Bearer <API_KEY>.

POST /events

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\\"}"
  }'

GET /events

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"

Environment

  • 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

Logging

  • 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

Database

  • Auto-migration runs on start for models.Event
  • Indexes on event_type, source, timestamp created in storage.InitPostgres

Testing

go test ./...

Project Structure

  • 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

About

A backend service that accepts high-volume events and stores them for later querying.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published