name: ${COMPOSE_PROJECT_NAME:-easyai-ai-gateway}

x-api-environment: &api-environment
  APP_ENV: ${AI_GATEWAY_COMPOSE_APP_ENV:-production}
  HTTP_ADDR: :8088
  AI_GATEWAY_DATABASE_URL: ${AI_GATEWAY_COMPOSE_DATABASE_URL:-postgresql://easyai:easyai2025@postgres:5432/easyai_ai_gateway?sslmode=disable}
  CONFIG_JWT_SECRET: ${CONFIG_JWT_SECRET:-this is a very secret secret}
  IDENTITY_MODE: ${AI_GATEWAY_COMPOSE_IDENTITY_MODE:-hybrid}
  IDENTITY_SECRET_STORE: file
  IDENTITY_SECRET_DIR: /app/data/identity/secrets
  IDENTITY_SECURITY_EVENTS_HEARTBEAT_INTERVAL_SECONDS: ${IDENTITY_SECURITY_EVENTS_HEARTBEAT_INTERVAL_SECONDS:-60}
  IDENTITY_SECURITY_EVENTS_STALE_AFTER_SECONDS: ${IDENTITY_SECURITY_EVENTS_STALE_AFTER_SECONDS:-180}
  IDENTITY_SECURITY_EVENTS_CLOCK_SKEW_SECONDS: ${IDENTITY_SECURITY_EVENTS_CLOCK_SKEW_SECONDS:-60}
  SERVER_MAIN_BASE_URL: ${AI_GATEWAY_COMPOSE_SERVER_MAIN_BASE_URL:-http://host.docker.internal:3000}
  SERVER_MAIN_INTERNAL_TOKEN: ${SERVER_MAIN_INTERNAL_TOKEN:-change-me}
  SERVER_MAIN_INTERNAL_KEY: ${SERVER_MAIN_INTERNAL_KEY:-gateway}
  SERVER_MAIN_INTERNAL_SECRET: ${SERVER_MAIN_INTERNAL_SECRET:-${SERVER_MAIN_INTERNAL_TOKEN:-change-me}}
  TASK_PROGRESS_CALLBACK_ENABLED: ${AI_GATEWAY_COMPOSE_TASK_PROGRESS_CALLBACK_ENABLED:-false}
  TASK_PROGRESS_CALLBACK_URL: ${AI_GATEWAY_COMPOSE_TASK_PROGRESS_CALLBACK_URL:-http://host.docker.internal:3000/internal/platform/task-progress-callbacks}
  TASK_PROGRESS_CALLBACK_TIMEOUT_MS: ${TASK_PROGRESS_CALLBACK_TIMEOUT_MS:-5000}
  TASK_PROGRESS_CALLBACK_MAX_ATTEMPTS: ${TASK_PROGRESS_CALLBACK_MAX_ATTEMPTS:-10}
  CORS_ALLOWED_ORIGIN: ${AI_GATEWAY_COMPOSE_CORS_ALLOWED_ORIGIN:-http://localhost:5178,http://127.0.0.1:5178}
  AI_GATEWAY_PUBLIC_BASE_URL: ${AI_GATEWAY_COMPOSE_PUBLIC_BASE_URL:-http://localhost:8088}
  AI_GATEWAY_WEB_BASE_URL: ${AI_GATEWAY_COMPOSE_WEB_BASE_URL:-http://localhost:5178}
  AI_GATEWAY_GENERATED_STORAGE_DIR: /app/data/static/generated
  AI_GATEWAY_UPLOADED_STORAGE_DIR: /app/data/static/uploaded

services:
  postgres:
    image: ${AI_GATEWAY_POSTGRES_IMAGE:-postgres:18-alpine}
    platform: ${AI_GATEWAY_PLATFORM:-linux/amd64}
    environment:
      POSTGRES_DB: ${AI_GATEWAY_COMPOSE_DATABASE_NAME:-easyai_ai_gateway}
      POSTGRES_USER: ${AI_GATEWAY_COMPOSE_PG_USER:-easyai}
      POSTGRES_PASSWORD: ${AI_GATEWAY_COMPOSE_PG_PASSWORD:-easyai2025}
    ports:
      - "${AI_GATEWAY_DB_PORT:-54329}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U \"$${POSTGRES_USER}\" -d \"$${POSTGRES_DB}\""]
      interval: 5s
      timeout: 5s
      retries: 20
      start_period: 10s
    restart: unless-stopped

  migrator:
    image: ${AI_GATEWAY_API_IMAGE:-${AI_GATEWAY_IMAGE_REGISTRY:-registry.cn-shanghai.aliyuncs.com/easyaigc}/ai-gateway:${AI_GATEWAY_IMAGE_TAG:-latest}}
    platform: ${AI_GATEWAY_PLATFORM:-linux/amd64}
    build:
      context: .
      dockerfile: Dockerfile
      target: api
      args:
        GOPROXY: ${AI_GATEWAY_GO_PROXY:-https://goproxy.cn,direct}
        GO_BUILD_IMAGE: ${AI_GATEWAY_GO_BUILD_IMAGE:-golang:1.26.3-alpine}
        API_RUNTIME_IMAGE: ${AI_GATEWAY_API_RUNTIME_IMAGE:-alpine:3.22}
    command: ["/app/easyai-ai-gateway-migrate"]
    environment: *api-environment
    extra_hosts:
      - "host.docker.internal:host-gateway"
    depends_on:
      postgres:
        condition: service_healthy
    restart: "no"

  api:
    image: ${AI_GATEWAY_API_IMAGE:-${AI_GATEWAY_IMAGE_REGISTRY:-registry.cn-shanghai.aliyuncs.com/easyaigc}/ai-gateway:${AI_GATEWAY_IMAGE_TAG:-latest}}
    platform: ${AI_GATEWAY_PLATFORM:-linux/amd64}
    build:
      context: .
      dockerfile: Dockerfile
      target: api
      args:
        GOPROXY: ${AI_GATEWAY_GO_PROXY:-https://goproxy.cn,direct}
        GO_BUILD_IMAGE: ${AI_GATEWAY_GO_BUILD_IMAGE:-golang:1.26.3-alpine}
        API_RUNTIME_IMAGE: ${AI_GATEWAY_API_RUNTIME_IMAGE:-alpine:3.22}
    environment: *api-environment
    ports:
      - "${AI_GATEWAY_API_PORT:-8088}:8088"
    volumes:
      - api_data:/app/data
    extra_hosts:
      - "host.docker.internal:host-gateway"
    depends_on:
      postgres:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:8088/api/v1/readyz | grep -q '\"ok\":true'"]
      interval: 10s
      timeout: 5s
      retries: 20
      start_period: 10s
    restart: unless-stopped

  web:
    image: ${AI_GATEWAY_WEB_IMAGE:-${AI_GATEWAY_IMAGE_REGISTRY:-registry.cn-shanghai.aliyuncs.com/easyaigc}/ai-gateway-web:${AI_GATEWAY_IMAGE_TAG:-latest}}
    platform: ${AI_GATEWAY_PLATFORM:-linux/amd64}
    build:
      context: .
      dockerfile: Dockerfile
      target: web
      args:
        VITE_GATEWAY_API_BASE_URL: ${AI_GATEWAY_WEB_API_BASE_URL:-/gateway-api}
        NODE_BUILD_IMAGE: ${AI_GATEWAY_NODE_BUILD_IMAGE:-node:22-alpine}
        WEB_RUNTIME_IMAGE: ${AI_GATEWAY_WEB_RUNTIME_IMAGE:-nginx:1.27-alpine}
        NPM_CONFIG_REGISTRY: ${AI_GATEWAY_NPM_REGISTRY:-https://registry.npmjs.org}
        VITE_BASE_PATH: ${AI_GATEWAY_WEB_BASE_PATH:-/}
    ports:
      - "${AI_GATEWAY_WEB_PORT:-5178}:80"
    volumes:
      - type: bind
        source: ./docker/nginx.conf
        target: /etc/nginx/conf.d/default.conf
        read_only: true
    depends_on:
      api:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1/api/v1/healthz | grep -q 'easyai-ai-gateway'"]
      interval: 10s
      timeout: 5s
      retries: 20
      start_period: 10s
    restart: unless-stopped

volumes:
  postgres_data:
  api_data:
