Skip to main content

API Mode

API mode runs only the HTTP server, making it ideal for horizontally scaled deployments behind a load balancer.

Configuration

services:
sercha-api:
image: sercha-core:latest
environment:
RUN_MODE: api
PORT: 8080
DATABASE_URL: postgres://...
REDIS_URL: redis://...
VESPA_URL: http://...
JWT_SECRET: ${JWT_SECRET}

What Runs

ComponentStatus
HTTP ServerYes
REST API EndpointsYes
Authentication MiddlewareYes
Task Queue ConsumerNo
SchedulerNo

Endpoints

API mode serves all REST endpoints:

CategoryEndpoints
Health/health, /ready, /version
Auth/api/v1/auth/login, /api/v1/auth/logout, /api/v1/auth/refresh
Search/api/v1/search
Sources/api/v1/sources/*
Documents/api/v1/documents/*
Users/api/v1/users/*, /api/v1/me
Settings/api/v1/settings/*
Admin/api/v1/admin/vespa/*

Health Checks

Configure container health checks using the built-in endpoints:

healthcheck:
test: ["CMD", "wget", "-q", "--spider", "http://localhost:8080/health"]
interval: 10s
timeout: 5s
retries: 3
EndpointPurpose
/healthBasic liveness check
/readyReadiness check (verifies database connection)

Scaling

API containers are stateless and can scale horizontally:

services:
sercha-api:
image: sercha-core:latest
environment:
RUN_MODE: api
# ... config
deploy:
replicas: 3

Place a load balancer in front of multiple API containers:

                    ┌─────────────────┐
│ Load Balancer │
└────────┬────────┘

┌───────────────────┼───────────────────┐
│ │ │
┌────▼────┐ ┌────▼────┐ ┌────▼────┐
│ API │ │ API │ │ API │
│ :8080 │ │ :8080 │ │ :8080 │
└─────────┘ └─────────┘ └─────────┘

Connection Pooling

When running multiple API containers, tune the database connection pool to avoid exhausting PostgreSQL connections:

environment:
DB_MAX_OPEN_CONNS: 10 # Per container
DB_MAX_IDLE_CONNS: 3 # Per container

Formula: Total DB connections = API replicas × DB_MAX_OPEN_CONNS

For 5 API containers with DB_MAX_OPEN_CONNS=10, ensure PostgreSQL allows at least 50 connections.

Graceful Shutdown

When the container receives SIGTERM:

  1. Stop accepting new connections
  2. Wait for in-flight requests to complete (up to 10 seconds)
  3. Close database and Redis connections
  4. Exit

Key Source Files

FileDescription
cmd/sercha-core/main.goMode selection and initialization
internal/adapters/driving/http/server.goHTTP server setup
internal/adapters/driving/http/handlers.goRequest handlers
internal/adapters/driving/http/middleware.goAuth and logging middleware

Next Steps