x-json-log-rotation: &json-log-rotation
  driver: json-file
  options:
    max-size: "20m"
    max-file: "5"

services:
  postgres:
    image: docker.m.daocloud.io/library/postgres:16-bookworm
    restart: unless-stopped
    logging: *json-log-rotation
    environment:
      TZ: ${TZ:-Asia/Shanghai}
      POSTGRES_DB: ${POSTGRES_DB:-order_site}
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "pg_isready -U ${POSTGRES_USER:-postgres} -d ${POSTGRES_DB:-order_site}",
        ]
      interval: 5s
      timeout: 5s
      retries: 20
    volumes:
      - postgres_data:/var/lib/postgresql/data

  backend:
    build:
      context: .
      dockerfile: deploy/docker/backend.Dockerfile
      args:
        # 默认阿里云公共源；阿里云 ECS 内网可设 DEBIAN_MIRROR=mirrors.cloud.aliyuncs.com
        DEBIAN_MIRROR: ${DEBIAN_MIRROR:-mirrors.aliyun.com}
        NPM_REGISTRY: ${NPM_REGISTRY:-https://registry.npmmirror.com}
    restart: unless-stopped
    env_file:
      - .env
    depends_on:
      postgres:
        condition: service_healthy
    healthcheck:
      test:
        ["CMD-SHELL", "curl -fsS http://127.0.0.1:3000/health/ready || exit 1"]
      interval: 5s
      timeout: 5s
      retries: 20
      start_period: 20s
    environment:
      TZ: ${TZ:-Asia/Shanghai}
      PORT: ${BACKEND_PORT:-3000}
      DATABASE_URL: ${DATABASE_URL:-postgres://postgres:postgres@postgres:5432/order_site}
      DATABASE_SSL: ${DATABASE_SSL:-false}
      DATABASE_MAX_CONNECTIONS: ${DATABASE_MAX_CONNECTIONS:-20}
      DATA_ROOT: /app/data
      LOG_RETENTION_DAYS: ${LOG_RETENTION_DAYS:-30}
      CLAIM_BASE_URL: ${CLAIM_BASE_URL:-http://localhost/#/claim}
      STORAGE_ENDPOINT: ${STORAGE_ENDPOINT:-http://minio:9000}
      STORAGE_BUCKET: ${STORAGE_BUCKET:-order-site}
      STORAGE_ACCESS_KEY_ID: ${STORAGE_ACCESS_KEY_ID:-minioadmin}
      STORAGE_SECRET_ACCESS_KEY: ${STORAGE_SECRET_ACCESS_KEY:-minioadmin}
      STORAGE_REGION: ${STORAGE_REGION:-us-east-1}
      STORAGE_MAX_UPLOAD_SIZE_MB: ${STORAGE_MAX_UPLOAD_SIZE_MB:-10}
      # ADMIN_SESSION_SECRET / ADMIN_DEFAULT_USERS_JSON 仅走 env_file。
      # 勿在此处用 ${ADMIN_DEFAULT_USERS_JSON} 再注入：JSON 会被 YAML 解析破坏，导致生产校验失败。
      KUASHOU_INDUSTRY_RATE_LIMIT_MAX: ${KUASHOU_INDUSTRY_RATE_LIMIT_MAX:-120}
    logging: *json-log-rotation
    volumes:
      - ./apps/backend/data:/app/data

  minio:
    image: docker.m.daocloud.io/minio/minio:latest
    profiles: ["storage"]
    restart: unless-stopped
    logging: *json-log-rotation
    command: server /data --address ":9000" --console-address ":9001"
    environment:
      TZ: ${TZ:-Asia/Shanghai}
      MINIO_ROOT_USER: ${STORAGE_ACCESS_KEY_ID:-minioadmin}
      MINIO_ROOT_PASSWORD: ${STORAGE_SECRET_ACCESS_KEY:-minioadmin}
    ports:
      - "9000:9000"
      - "9001:9001"
    volumes:
      - minio_data:/data

  web:
    build:
      context: .
      dockerfile: deploy/docker/frontend.Dockerfile
      args:
        APP_DOMAIN: ${APP_DOMAIN:-localhost}
        ALPINE_MIRROR: ${ALPINE_MIRROR:-mirrors.aliyun.com}
        NPM_REGISTRY: ${NPM_REGISTRY:-https://registry.npmmirror.com}
    restart: unless-stopped
    logging: *json-log-rotation
    depends_on:
      backend:
        condition: service_healthy
    ports:
      - "80:80"
      - "443:443"
    environment:
      TZ: ${TZ:-Asia/Shanghai}
      APP_DOMAIN: ${APP_DOMAIN:-localhost}
      CADDY_SITE_ADDR: ${CADDY_SITE_ADDR:-http://localhost}
    volumes:
      - caddy_data:/data
      - caddy_config:/config

volumes:
  postgres_data:
  minio_data:
  caddy_data:
  caddy_config:
