# LLM Gateway - Docker Compose Configuration
# Download from: https://llmgateway.deep-cells.com/downloads/docker-compose/docker-compose.yml
# Documentation: https://llmgateway.deep-cells.com/downloads/

services:
  # LLM Gateway Application
  llm-gateway:
    image: deepcells/llm-gateway:latest
    container_name: llm-gateway
    restart: unless-stopped
    ports:
      - "3000:3000"
    environment:
      # Database Configuration
      - SQL_DSN=postgres://llmgateway:${POSTGRES_PASSWORD:-123456}@postgres:5432/llmgateway?sslmode=disable
      
      # Redis Configuration  
      - REDIS_CONN_STRING=redis://:${REDIS_PASSWORD:-123456}@redis:6379/0
      
      # Application Configuration
      - SESSION_SECRET=${SESSION_SECRET:-your-32-char-secret-key-here-change-this}
      - TZ=Asia/Shanghai
      - GIN_MODE=release
      
      # Optional: Sync frequency for multi-instance deployment
      - SYNC_FREQUENCY=60
      
      # Optional: For multi-node deployment
      # - NODE_TYPE=slave
      # - FRONTEND_BASE_URL=https://your-domain.com
    volumes:
      - ./data/llmgateway:/data
      - ./logs:/app/logs
    depends_on:
      - postgres
      - redis
    networks:
      - llm-gateway-network
    healthcheck:
      test: ["CMD-SHELL", "wget -q -O - http://localhost:3000/api/status || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  # PostgreSQL Database
  postgres:
    image: postgres:18-alpine
    container_name: llm-gateway-postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: llmgateway
      POSTGRES_USER: llmgateway
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-123456}
      TZ: Asia/Shanghai
      # Optional: If you need a superuser (not recommended for production)
      # POSTGRES_SUPERUSER: ${POSTGRES_SUPERUSER:-postgres}
      # POSTGRES_SUPERUSER_PASSWORD: ${POSTGRES_SUPERUSER_PASSWORD:-super123456}
    volumes:
      - postgres_data:/var/lib/postgresql
      - ./logs/postgres:/var/log/postgresql
    # ports:
    #   Uncomment to access PostgreSQL externally (not recommended for production)
    #   - "5432:5432"
    command: >
      postgres
      -c listen_addresses=*
      -c max_connections=100
      -c shared_buffers=256MB
      -c effective_cache_size=1GB
      -c maintenance_work_mem=64MB
      -c checkpoint_completion_target=0.9
      -c wal_buffers=16MB
      -c default_statistics_target=100
      -c random_page_cost=1.1
      -c effective_io_concurrency=200
      -c work_mem=4MB
      -c min_wal_size=1GB
      -c max_wal_size=4GB
      -c log_min_duration_statement=1000
      -c log_checkpoints=on
      -c log_connections=on
      -c log_disconnections=on
      -c log_lock_waits=on
      -c log_temp_files=0
    networks:
      - llm-gateway-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U llmgateway"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Redis Cache
  redis:
    image: redis:7-alpine
    container_name: llm-gateway-redis
    restart: unless-stopped
    command: >
      redis-server 
      --requirepass ${REDIS_PASSWORD:-123456}
      --appendonly yes
      --save 900 1
      --save 300 10
      --save 60 10000
      --maxmemory 256mb
      --maxmemory-policy allkeys-lru
    volumes:
      - redis_data:/data
      - ./logs/redis:/var/log/redis
    # ports:
    #   Uncomment to access Redis externally (not recommended for production)
    #   - "6379:6379"
    networks:
      - llm-gateway-network

  # Optional: Nginx Reverse Proxy (for production deployment)
  nginx:
    image: nginx:alpine
    container_name: llm-gateway-nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/conf.d:/etc/nginx/conf.d:ro
      - ./ssl:/etc/nginx/ssl:ro
      - ./certbot:/var/www/certbot:ro
      - ./logs/nginx:/var/log/nginx
    depends_on:
      - llm-gateway
    networks:
      - llm-gateway-network
    profiles:
      # This service is optional - enable with: docker-compose --profile production up
      - production

networks:
  llm-gateway-network:
    driver: bridge

volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local