# 生产部署专用 Compose 文件
# 由 Gitea Actions 在 /opt/agentkit/repo 下调用
# 与开发用 docker-compose.yaml 的区别：
#   1. 不暴露 Redis/PostgreSQL 端口到公网（仅内部通信）
#   2. 密码通过 .env 注入
#   3. 配置日志大小限制，避免磁盘打满
#   4. 配置资源限制，避免单服务吃满内存

services:
  agentkit:
    build: .
    command: serve --host 0.0.0.0 --port 8001
    ports:
      - "8001:8001"
    env_file: .env
    environment:
      - REDIS_URL=redis://redis:6379/0
      - DATABASE_URL=postgresql+asyncpg://agentkit:${POSTGRES_PASSWORD}@postgres:5432/agentkit
    depends_on:
      redis:
        condition: service_healthy
      postgres:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8001/api/v1/health')"]
      interval: 30s
      timeout: 10s
      start_period: 30s
      retries: 3
    restart: unless-stopped
    logging:
      driver: json-file
      options:
        max-size: "50m"
        max-file: "5"
    deploy:
      resources:
        limits:
          memory: 2G

  redis:
    image: redis:7-alpine
    # 不暴露端口到公网，仅容器内部通信
    expose:
      - "6379"
    command: >
      redis-server
      --requirepass ${REDIS_PASSWORD}
      --maxmemory 256mb
      --maxmemory-policy allkeys-lru
    volumes:
      - redisdata:/data
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD}", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "3"

  postgres:
    image: pgvector/pgvector:pg15
    expose:
      - "5432"
    environment:
      POSTGRES_USER: agentkit
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: agentkit
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U agentkit"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "3"

volumes:
  redisdata:
  pgdata:
