Sign inSign up

petdog/battlescope-api

By petdog

Updated 10 months ago

BattleScope api service

Image
0

4.5K

petdog/battlescope-api repository overview

BattleScope API Docker Image

Image Name: petdog/battlescope-api:latest

Source: Dockerfile with build args SERVICE_SCOPE=@battlescope/api and BUILD_TARGET=backend/api

Purpose

REST API gateway providing access to battle data, killmail information, search functionality, and administrative operations. Built with Fastify for high performance.

Features

  • RESTful API with OpenAPI/Swagger documentation
  • EVE Online SSO authentication (OAuth2)
  • Feature-based RBAC authorization
  • Real-time killmail stream via Server-Sent Events (SSE)
  • Full-text search via Typesense
  • Rate limiting and request validation
  • Comprehensive observability (logs, metrics, traces)

Configuration

Environment Variables
Core Configuration
VariableDescriptionDefaultRequired
PORTHTTP port to listen on3000No
HOSTHost to bind to0.0.0.0No
DEVELOPER_MODEEnable developer mode (verbose logging)falseNo
CORS_ALLOWED_ORIGINSComma-separated list of allowed CORS origins[]No
LOG_LEVELLogging level (trace, debug, info, warn, error)infoNo
Database Configuration
VariableDescriptionDefaultRequired
DATABASE_URLPostgreSQL connection string-Yes
POSTGRES_HOSTPostgreSQL hostlocalhostIf no DATABASE_URL
POSTGRES_PORTPostgreSQL port5432No
POSTGRES_DBDatabase namebattlescopeNo
POSTGRES_USERDatabase user-If no DATABASE_URL
POSTGRES_PASSWORDDatabase password-If no DATABASE_URL
POSTGRES_SSLEnable SSL connectionfalseNo
Redis Configuration
VariableDescriptionDefaultRequired
ESI_REDIS_CACHE_URLRedis URL for ESI API caching-No
SESSION_REDIS_URLRedis URL for session storage-Yes (for auth)
EVE Online API (ESI) Configuration
VariableDescriptionDefaultRequired
ESI_BASE_URLESI API base URLhttps://esi.evetech.net/latest/No
ESI_DATASOURCEESI datasourcetranquilityNo
ESI_COMPATIBILITY_DATEESI compatibility date2025-09-30No
ESI_TIMEOUT_MSESI request timeout (ms)10000No
ESI_CACHE_TTL_SECONDSESI cache TTL300No
Authentication Configuration
VariableDescriptionDefaultRequired
EVE_CLIENT_IDEVE OAuth2 client ID-Yes (for auth)
EVE_CLIENT_SECRETEVE OAuth2 client secret-Yes (for auth)
EVE_CALLBACK_URLOAuth2 callback URL-Yes (for auth)
EVE_SCOPESSpace-separated OAuth2 scopespublicDataNo
ENCRYPTION_KEY32-byte encryption key (base64) for token storage-Yes (for auth)
SESSION_TTL_SECONDSSession lifetime28800 (8 hours)No
SESSION_COOKIE_NAMESession cookie namebattlescope_sessionNo
SESSION_COOKIE_SECURERequire HTTPS for cookiestrueNo
AUTHZ_CACHE_TTL_SECONDSAuthorization cache TTL60No
FRONTEND_URLFrontend URL for redirectshttp://localhost:5173Yes (for auth)
Search Configuration
VariableDescriptionDefaultRequired
TYPESENSE_HOSTTypesense server hosttypesense.battlescope.svc.cluster.localYes
TYPESENSE_PORTTypesense server port8108No
TYPESENSE_PROTOCOLTypesense protocol (http/https)httpNo
TYPESENSE_API_KEYTypesense API keybattlescope-search-keyYes
Observability Configuration
VariableDescriptionDefaultRequired
OTEL_EXPORTER_OTLP_ENDPOINTOpenTelemetry collector endpointhttp://otel-collector:4318No
OTEL_SERVICE_NAMEService name for telemetrybattlescope-apiNo
OTEL_METRIC_EXPORT_INTERVALMetrics export interval (ms)15000No

Ports

PortProtocolDescription
3000HTTPREST API and Swagger UI

Health Checks

# HTTP health check endpoint
curl http://localhost:3000/healthz

Returns 200 OK with { "status": "ok" } when healthy.

API Endpoints

Core Endpoints
  • GET /healthz - Health check
  • GET /docs - Swagger UI documentation
  • GET /openapi.json - OpenAPI specification
Battle Endpoints
  • GET /battles - List battles with filtering
  • GET /battles/:id - Get battle details
  • GET /battles/:id/killmails - Get battle killmails
Killmail Endpoints
  • GET /killmails/recent - Get recent killmails
  • GET /killmails/stream - SSE stream of new killmails
Search Endpoints
  • POST /search/battles - Search battles
  • GET /search/entities - Search entities (characters, corps, alliances)
Admin Endpoints
  • GET /admin/rulesets - List ingestion rulesets
  • POST /admin/rulesets - Create ruleset
  • PUT /admin/rulesets/:id - Update ruleset
  • DELETE /admin/rulesets/:id - Delete ruleset
  • GET /admin/stats - System statistics
Authentication Endpoints
  • GET /auth/login - Initiate EVE SSO login
  • GET /auth/callback - OAuth2 callback
  • POST /auth/logout - Logout
  • GET /auth/me - Get current user

Example Usage

Docker Run
docker run -d \
  --name battlescope-api \
  -p 3000:3000 \
  -e DATABASE_URL=postgres://user:pass@postgres:5432/battlescope \
  -e REDIS_URL=redis://redis:6379/0 \
  -e SESSION_REDIS_URL=redis://redis:6379/2 \
  -e EVE_CLIENT_ID=your-client-id \
  -e EVE_CLIENT_SECRET=your-secret \
  -e EVE_CALLBACK_URL=http://localhost:3000/auth/callback \
  -e ENCRYPTION_KEY=$(openssl rand -base64 32) \
  -e FRONTEND_URL=http://localhost:5173 \
  -e TYPESENSE_HOST=typesense \
  -e TYPESENSE_API_KEY=your-api-key \
  petdog/battlescope-api:latest
Docker Compose
services:
  api:
    image: petdog/battlescope-api:latest
    ports:
      - "3000:3000"
    environment:
      PORT: 3000
      HOST: 0.0.0.0
      DATABASE_URL: postgres://battlescope:password@postgres:5432/battlescope
      REDIS_URL: redis://redis:6379/0
      SESSION_REDIS_URL: redis://redis:6379/2
      ESI_REDIS_CACHE_URL: redis://redis:6379/1
      EVE_CLIENT_ID: ${EVE_CLIENT_ID}
      EVE_CLIENT_SECRET: ${EVE_CLIENT_SECRET}
      EVE_CALLBACK_URL: http://localhost:3000/auth/callback
      ENCRYPTION_KEY: ${ENCRYPTION_KEY}
      FRONTEND_URL: http://localhost:5173
      TYPESENSE_HOST: typesense
      TYPESENSE_PORT: 8108
      TYPESENSE_API_KEY: ${TYPESENSE_API_KEY}
      SESSION_COOKIE_SECURE: false
    depends_on:
      - postgres
      - redis
      - typesense
    restart: unless-stopped
Kubernetes Deployment

See infra/k8s/api-deployment.yaml for the complete Kubernetes manifest.

Resource Requirements

  • CPU: 200m request, 1000m limit
  • Memory: 256Mi request, 1Gi limit
Minimum
  • CPU: 50m
  • Memory: 128Mi

Dependencies

Required Services
  • PostgreSQL 15+: Primary database
  • Redis 7+: Session storage and optional ESI caching
  • Typesense: Search engine
Optional Services
  • OpenTelemetry Collector: For metrics and tracing
  • Loki: For log aggregation

Build Information

Build Command
docker build \
  --build-arg SERVICE_SCOPE=@battlescope/api \
  --build-arg BUILD_TARGET=backend/api \
  -t petdog/battlescope-api:latest \
  -f Dockerfile \
  .

Troubleshooting

Authentication Not Working
  1. Verify all auth environment variables are set
  2. Check ENCRYPTION_KEY is 32+ characters
  3. Verify EVE_CALLBACK_URL matches your EVE application configuration
  4. Check Redis connectivity for session storage
Database Connection Fails
  1. Verify DATABASE_URL format: postgres://user:pass@host:port/dbname
  2. Check network connectivity to PostgreSQL
  3. Verify database exists and migrations have run
Search Not Working
  1. Verify Typesense is running and accessible
  2. Check TYPESENSE_API_KEY matches Typesense configuration
  3. Ensure search-sync service has indexed data

Security Considerations

  • Always use strong ENCRYPTION_KEY (32+ random bytes, base64 encoded)
  • Set SESSION_COOKIE_SECURE=true in production with HTTPS
  • Use Redis password authentication in production
  • Configure CORS_ALLOWED_ORIGINS to restrict API access
  • Rotate EVE_CLIENT_SECRET and ENCRYPTION_KEY periodically
  • Use Kubernetes Secrets for sensitive environment variables

Version Information

  • Node.js: 20 LTS
  • Fastify: 4.26
  • TypeScript: 5.4.5

Additional Resources

Tag summary

Content type

Image

Digest

sha256:e2091a979

Size

100.1 MB

Last updated

10 months ago

docker pull petdog/battlescope-api