x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

services:
  # Kong Database
  kong-db:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}postgres:${POSTGRES_VERSION:-16.6-alpine3.21}
    volumes:
      - kong_db_data:/var/lib/postgresql/data
    environment:
      - TZ=${TZ:-UTC}
      - POSTGRES_USER=kong
      - POSTGRES_DB=kong
      - POSTGRES_PASSWORD=${KONG_DB_PASSWORD:-kongpass}
    deploy:
      resources:
        limits:
          cpus: ${KONG_DB_CPU_LIMIT:-1.0}
          memory: ${KONG_DB_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${KONG_DB_CPU_RESERVATION:-0.1}
          memory: ${KONG_DB_MEMORY_RESERVATION:-128M}
    healthcheck:
      test: [CMD-SHELL, pg_isready -U kong]
      interval: 30s
      timeout: 5s
      retries: 5
      start_period: 30s

  # Kong Database Migration
  kong-migrations:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}kong:${KONG_VERSION:-3.8.0-alpine}
    depends_on:
      kong-db:
        condition: service_healthy
    environment:
      - TZ=${TZ:-UTC}
      - KONG_DATABASE=postgres
      - KONG_PG_HOST=kong-db
      - KONG_PG_USER=kong
      - KONG_PG_PASSWORD=${KONG_DB_PASSWORD:-kongpass}
      - KONG_PG_DATABASE=kong
    command: kong migrations bootstrap
    restart: no

  # Kong Gateway
  kong:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}kong:${KONG_VERSION:-3.8.0-alpine}
    depends_on:
      kong-db:
        condition: service_healthy
      kong-migrations:
        condition: service_completed_successfully
    ports:
      - '${KONG_PROXY_PORT_OVERRIDE:-8000}:8000'
      - '${KONG_PROXY_SSL_PORT_OVERRIDE:-8443}:8443'
      - '${KONG_ADMIN_API_PORT_OVERRIDE:-8001}:8001'
      - '${KONG_ADMIN_SSL_PORT_OVERRIDE:-8444}:8444'

      # Custom configuration
      # - ./kong.conf:/etc/kong/kong.conf:ro
    environment:
      - TZ=${TZ:-UTC}
      - KONG_DATABASE=postgres
      - KONG_PG_HOST=kong-db
      - KONG_PG_USER=kong
      - KONG_PG_PASSWORD=${KONG_DB_PASSWORD:-kongpass}
      - KONG_PG_DATABASE=kong
      - KONG_PROXY_ACCESS_LOG=/dev/stdout
      - KONG_ADMIN_ACCESS_LOG=/dev/stdout
      - KONG_PROXY_ERROR_LOG=/dev/stderr
      - KONG_ADMIN_ERROR_LOG=/dev/stderr
      - KONG_ADMIN_LISTEN=${KONG_ADMIN_LISTEN:-0.0.0.0:8001}
      - KONG_ADMIN_GUI_URL=${KONG_ADMIN_GUI_URL:-http://localhost:8002}
    deploy:
      resources:
        limits:
          cpus: ${KONG_CPU_LIMIT:-1.0}
          memory: ${KONG_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${KONG_CPU_RESERVATION:-0.1}
          memory: ${KONG_MEMORY_RESERVATION:-256M}
    healthcheck:
      test: [CMD-SHELL, kong health]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s

  # Kong Manager (Optional GUI)
  kong-gui:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}pantsel/konga:${KONGA_VERSION:-latest}
    depends_on:
      kong:
        condition: service_healthy
    ports:
      - '${KONG_GUI_PORT_OVERRIDE:-1337}:1337'
    volumes:
      - konga_data:/app/kongadata
    environment:
      - TZ=${TZ:-UTC}
      - NODE_ENV=production
      - KONGA_HOOK_TIMEOUT=120000
    deploy:
      resources:
        limits:
          cpus: ${KONGA_CPU_LIMIT:-0.5}
          memory: ${KONGA_MEMORY_LIMIT:-256M}
        reservations:
          cpus: ${KONGA_CPU_RESERVATION:-0.1}
          memory: ${KONGA_MEMORY_RESERVATION:-64M}
    profiles:
      - gui

volumes:
  kong_db_data:
  konga_data:
