version: "3.8"

services:
  postgres:
    image: postgres:16-alpine
    container_name: postgres
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
      POSTGRES_DB: ${POSTGRES_DB:-flower}
    # 仅绑定到本机，避免把数据库暴露到公网
    ports:
      - "127.0.0.1:${POSTGRES_PORT:-5432}:5432"
    volumes:
      - db_data:/var/lib/postgresql/data
    restart: unless-stopped

  pgbouncer:
    image: edoburu/pgbouncer:latest
    container_name: pgbouncer
    ports:
      - "127.0.0.1:6432:6432"
    volumes:
      - ./deploy/pgbouncer/pgbouncer.ini:/etc/pgbouncer/pgbouncer.ini
      - ./deploy/pgbouncer/userlist.txt:/etc/pgbouncer/userlist.txt
    depends_on:
      - postgres
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    container_name: redis
    volumes:
      - redis_data:/data
    restart: unless-stopped

  rabbitmq:
    image: rabbitmq:3-management-alpine
    container_name: rabbitmq
    environment:
      RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER:-guest}
      RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS:-guest}
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
    restart: unless-stopped

  web:
    image: ${FLOWER_IMAGE:-flower-app:latest}
    container_name: flower_web
    command:
      - uvicorn
      - flower.asgi:application
      - --host
      - 0.0.0.0
      - --port
      - "8000"
      - --workers
      - "${UVICORN_WORKERS:-1}"
    ports:
      - "${WEB_PORT:-8100}:8000"
    environment:
      DJANGO_SETTINGS_MODULE: flower.settings
      PYTHONPATH: /app
      DEBUG: "${DEBUG:-0}"
      ALLOWED_HOSTS: "${ALLOWED_HOSTS:-yuwenerp.yuwen.cloud}"
      DB_HOST: ${DB_HOST:-pgbouncer}
      DB_PORT: "${DB_PORT:-6432}"
      DB_NAME: "${DB_NAME:-flower}"
      DB_USER: "${DB_USER:-postgres}"
      DB_PASSWORD: "${DB_PASSWORD:-postgres}"
      CACHE_HOST: ${CACHE_HOST:-redis}
      CACHE_PORT: "${CACHE_PORT:-6379}"
      # Celery Broker（RabbitMQ）连接：容器内用服务名访问
      CELERY_BROKER_HOST: ${CELERY_BROKER_HOST:-rabbitmq}
      CELERY_BROKER_PORT: "${CELERY_BROKER_PORT:-5672}"
      CELERY_BROKER_USER: "${CELERY_BROKER_USER:-guest}"
      CELERY_BROKER_PASSWORD: "${CELERY_BROKER_PASSWORD:-guest}"
      # Celery Result Backend（Redis）连接：容器内用服务名访问
      CELERY_RESULT_HOST: ${CELERY_RESULT_HOST:-redis}
      CELERY_RESULT_PORT: "${CELERY_RESULT_PORT:-6379}"
      CELERY_RESULT_DB: "${CELERY_RESULT_DB:-1}"
    depends_on:
      - pgbouncer
      - redis
      - rabbitmq
    restart: unless-stopped

  celery_worker:
    image: ${FLOWER_IMAGE:-flower-app:latest}
    container_name: celery_worker
    command:
      - celery
      - -A
      - flower
      - worker
      - -l
      - info
      - --concurrency=${CELERY_CONCURRENCY:-2}
      - --pool=solo
    environment:
      PYTHONPATH: /app
      DB_HOST: ${DB_HOST:-pgbouncer}
      DB_PORT: "${DB_PORT:-6432}"
      DB_NAME: "${DB_NAME:-flower}"
      DB_USER: "${DB_USER:-postgres}"
      DB_PASSWORD: "${DB_PASSWORD:-postgres}"
      # 缓存（Redis）连接：容器内用服务名访问
      CACHE_HOST: ${CACHE_HOST:-redis}
      CACHE_PORT: "${CACHE_PORT:-6379}"
      # Celery 连接配置：容器内用服务名访问
      CELERY_BROKER_HOST: ${CELERY_BROKER_HOST:-rabbitmq}
      CELERY_BROKER_PORT: "${CELERY_BROKER_PORT:-5672}"
      CELERY_BROKER_USER: "${CELERY_BROKER_USER:-guest}"
      CELERY_BROKER_PASSWORD: "${CELERY_BROKER_PASSWORD:-guest}"
      CELERY_RESULT_HOST: ${CELERY_RESULT_HOST:-redis}
      CELERY_RESULT_PORT: "${CELERY_RESULT_PORT:-6379}"
      CELERY_RESULT_DB: "${CELERY_RESULT_DB:-1}"
    # 数据库备份输出目录（api_v1.tasks.backup_database 默认输出到 BASE_DIR/data-bak）
    volumes:
      # 默认落在 compose 同级目录的 ./data-bak，方便宿主机直接查看/拷贝
      # 也可通过 BACKUP_DIR 指定绝对路径，如 /srv/flower/data-bak
      - ${BACKUP_DIR:-./data-bak}:/app/data-bak
    depends_on:
      - pgbouncer
      - redis
      - rabbitmq
    restart: unless-stopped

  celery_beat:
    image: ${FLOWER_IMAGE:-flower-app:latest}
    container_name: celery_beat
    command:
      - celery
      - -A
      - flower
      - beat
      - -l
      - info
      - --schedule
      - /var/lib/celery/celerybeat-schedule
    environment:
      DJANGO_SETTINGS_MODULE: flower.settings
      PYTHONPATH: /app
      DB_HOST: ${DB_HOST:-pgbouncer}
      DB_PORT: "${DB_PORT:-6432}"
      DB_NAME: "${DB_NAME:-flower}"
      DB_USER: "${DB_USER:-postgres}"
      DB_PASSWORD: "${DB_PASSWORD:-postgres}"
      # 缓存（Redis）连接：容器内用服务名访问
      CACHE_HOST: ${CACHE_HOST:-redis}
      CACHE_PORT: "${CACHE_PORT:-6379}"
      # Celery 连接配置：容器内用服务名访问
      CELERY_BROKER_HOST: ${CELERY_BROKER_HOST:-rabbitmq}
      CELERY_BROKER_PORT: "${CELERY_BROKER_PORT:-5672}"
      CELERY_BROKER_USER: "${CELERY_BROKER_USER:-guest}"
      CELERY_BROKER_PASSWORD: "${CELERY_BROKER_PASSWORD:-guest}"
      CELERY_RESULT_HOST: ${CELERY_RESULT_HOST:-redis}
      CELERY_RESULT_PORT: "${CELERY_RESULT_PORT:-6379}"
      CELERY_RESULT_DB: "${CELERY_RESULT_DB:-1}"
    # Celery Beat 的持久化调度文件，避免重启后重复/错过调度
    volumes:
      - celery_beat_data:/var/lib/celery
    depends_on:
      - postgres
      - redis
      - rabbitmq
    restart: unless-stopped

volumes:
  db_data:
  redis_data:
  rabbitmq_data:
  celery_beat_data:

