# HZHub-AI 一键启动全部服务
# 使用方式: docker-compose up -d
#
# 包含服务:
#   - MySQL 8.0 (数据库，包含初始化SQL)
#   - Redis 6.2 (缓存)
#   - Weaviate (向量数据库)
#   - MinIO (对象存储)
#   - HZHub-Backend (后端服务)
#   - HZHub-Admin (管理端前端)
#   - HZHub-Web (用户端前端)
#
# 镜像仓库地址: crpi-31mraxd99y2gqdgr.cn-beijing.personal.cr.aliyuncs.com/ruoyi_ai

version: '3.8'

services:
  # ==================== MySQL 数据库 ====================
  mysql:
    # 阿里云镜像地址（包含初始化SQL）
    image: crpi-31mraxd99y2gqdgr.cn-beijing.personal.cr.aliyuncs.com/ruoyi_ai/mysql:v3
    container_name: hzhub-ai-mysql
    restart: always
    ports:
      - '23306:3306'
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: hzhub-ai-agent
      TZ: Asia/Shanghai
    volumes:
      - mysql-data:/var/lib/mysql
    healthcheck:
      test: [CMD, mysqladmin, ping, -h, localhost, -u, root, -proot]
      interval: 15s
      timeout: 10s
      retries: 10
      start_period: 60s
    networks:
      - ruoyi-net

  # ==================== Redis 缓存 ====================
  redis:
    image: crpi-31mraxd99y2gqdgr.cn-beijing.personal.cr.aliyuncs.com/ruoyi_ai/redis:6.2
    container_name: hzhub-ai-redis
    restart: always
    ports:
      - '26379:6379'
    volumes:
      - redis-data:/data
    command: redis-server --appendonly yes
    healthcheck:
      test: [CMD, redis-cli, ping]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - ruoyi-net

  # ==================== Weaviate 向量数据库 ====================
  weaviate:
    image: crpi-31mraxd99y2gqdgr.cn-beijing.personal.cr.aliyuncs.com/ruoyi_ai/weaviate:1.30.0
    container_name: hzhub-ai-weaviate
    restart: always
    ports:
      - '28080:8080'
    environment:
      QUERY_DEFAULTS_LIMIT: 25
      AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED: true
      PERSISTENCE_DATA_PATH: /var/lib/weaviate
      DEFAULT_VECTORIZER_MODULE: none
      ENABLE_MODULES: text2vec-cohere,text2vec-huggingface,text2vec-palm,text2vec-openai,generative-openai,generative-cohere,generative-palm,ref2vec-centroid,reranker-cohere,qna-openai
      CLUSTER_HOSTNAME: node1
    volumes:
      - weaviate-data:/var/lib/weaviate
    networks:
      - ruoyi-net

  # ==================== MinIO 对象存储 ====================
  minio:
    image: crpi-31mraxd99y2gqdgr.cn-beijing.personal.cr.aliyuncs.com/ruoyi_ai/minio:latest
    container_name: hzhub-ai-minio
    restart: always
    ports:
      - '29000:9000'
      - '29090:9090'
    environment:
      MINIO_ROOT_USER: ruoyi
      MINIO_ROOT_PASSWORD: ruoyi123
    volumes:
      - minio-data:/data
    command: server /data --console-address ":9090"
    networks:
      - ruoyi-net

  # ==================== HZHub-AI 后端服务 ====================
  backend:
    image: crpi-31mraxd99y2gqdgr.cn-beijing.personal.cr.aliyuncs.com/ruoyi_ai/hzhub-ai-backend:latest
    container_name: hzhub-ai-backend
    restart: always
    ports:
      - '26039:6039'
    environment:
      TZ: Asia/Shanghai
      # MySQL 配置
      SPRING_DATASOURCE_DYNAMIC_PRIMARY: master
      SPRING_DATASOURCE_DYNAMIC_DATASOURCE_MASTER_DRIVERCLASSNAME: com.mysql.cj.jdbc.Driver
      SPRING_DATASOURCE_DYNAMIC_DATASOURCE_MASTER_URL: jdbc:mysql://mysql:3306/hzhub-ai-agent?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true
      SPRING_DATASOURCE_DYNAMIC_DATASOURCE_MASTER_USERNAME: root
      SPRING_DATASOURCE_DYNAMIC_DATASOURCE_MASTER_PASSWORD: root
      # Redis 配置
      SPRING_DATA_REDIS_HOST: redis
      SPRING_DATA_REDIS_PORT: 6379
      SPRING_DATA_REDIS_DATABASE: 0
      # 日志配置
      LOGGING_LEVEL_ORG_RUOYI: info
      LOGGING_LEVEL_ORG_SPRINGFRAMEWORK: warn
      SYS_UPLOAD_PATH: /ruoyi/upload
    volumes:
      - logs-data:/ruoyi/server/logs
      - upload-data:/ruoyi/upload
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_started
    networks:
      - ruoyi-net

  # ==================== HZHub-AI 管理端前端 ====================
  admin-frontend:
    image: crpi-31mraxd99y2gqdgr.cn-beijing.personal.cr.aliyuncs.com/ruoyi_ai/hzhub-ai-admin:latest
    container_name: hzhub-ai-admin
    restart: always
    ports:
      - '25666:5666'
    environment:
      # 后端 API 地址 - 运行时动态配置（无需重新构建镜像）
      # 在完整 docker-compose 中应设置为: http://backend:6039
      # 独立运行时可设置为实际后端地址，如: 192.168.1.100:6039
      UPSTREAM_HOST: backend:6039
    # 资源限制 - 防止 CPU 和内存耗尽
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 3G
        reservations:
          cpus: '1'
          memory: 1G
    depends_on:
      - backend
    networks:
      - ruoyi-net

  # ==================== HZHub-AI 用户端前端 ====================
  web-frontend:
    image: crpi-31mraxd99y2gqdgr.cn-beijing.personal.cr.aliyuncs.com/ruoyi_ai/hzhub-ai-web:latest
    container_name: hzhub-ai-web
    restart: always
    ports:
      - '25137:5137'
    environment:
      UPSTREAM_URL: http://backend:6039
    depends_on:
      - backend
    networks:
      - ruoyi-net

# ==================== 网络配置 ====================
networks:
  ruoyi-net:
    driver: bridge

# ==================== 数据卷配置 ====================
volumes:
  mysql-data:
  redis-data:
  weaviate-data:
  minio-data:
  logs-data:
  upload-data:
