# =============================================================================
# MinIO Object Storage - Docker Compose
# =============================================================================
#
# MinIO 是一个高性能的分布式对象存储系统，兼容 Amazon S3 API
#
# 用途:
#   - 用户头像存储
#   - 用户文档存储
#   - 应用资源文件存储
#   - 备份文件存储
#
# 使用方法:
#   ./deploy.sh up          # 启动 MinIO
#   ./deploy.sh down        # 停止 MinIO
#   ./deploy.sh status      # 查看状态
#   ./deploy.sh logs        # 查看日志
#   ./deploy.sh create-bucket <name>  # 创建存储桶
#
# 访问地址:
#   API:     http://localhost:9000
#   Console: http://localhost:9001
#
# =============================================================================

services:
  # ===========================================================================
  # MinIO Object Storage
  # ===========================================================================
  minio:
    image: docker.io/minio/minio:RELEASE.2024-11-07T00-52-20Z
    container_name: rwa-minio
    command: server /data --console-address ":9001"
    environment:
      TZ: Asia/Shanghai
      # 管理员凭证
      MINIO_ROOT_USER: ${MINIO_ROOT_USER:-admin}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minio_secret_password}
      # 服务器配置
      MINIO_BROWSER: "on"
      MINIO_BROWSER_REDIRECT_URL: ${MINIO_CONSOLE_URL:-http://localhost:9001}
      # 区域设置
      MINIO_REGION: ${MINIO_REGION:-cn-east-1}
      # 性能优化
      MINIO_API_REQUESTS_DEADLINE: 10s
      MINIO_API_REQUESTS_MAX: 10000
    ports:
      - "${MINIO_API_PORT:-9000}:9000"       # S3 API
      - "${MINIO_CONSOLE_PORT:-9001}:9001"   # Web Console
    volumes:
      - minio_data:/data
    healthcheck:
      test: ["CMD", "mc", "ready", "local"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s
    restart: unless-stopped
    networks:
      - rwa-minio

  # ===========================================================================
  # MinIO Client (mc) - 初始化配置
  # ===========================================================================
  # 用于创建默认存储桶和配置访问策略
  # ===========================================================================
  minio-init:
    image: docker.io/minio/mc:RELEASE.2024-11-05T11-29-45Z
    container_name: rwa-minio-init
    depends_on:
      minio:
        condition: service_healthy
    environment:
      MINIO_ROOT_USER: ${MINIO_ROOT_USER:-admin}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minio_secret_password}
      # 存储桶配置
      BUCKET_AVATARS: ${BUCKET_AVATARS:-avatars}
      BUCKET_DOCUMENTS: ${BUCKET_DOCUMENTS:-documents}
      BUCKET_RESOURCES: ${BUCKET_RESOURCES:-resources}
      BUCKET_BACKUPS: ${BUCKET_BACKUPS:-backups}
    entrypoint: >
      /bin/sh -c "
        echo 'Waiting for MinIO to be ready...';
        sleep 5;

        echo 'Configuring MinIO client...';
        mc alias set rwa http://minio:9000 $${MINIO_ROOT_USER} $${MINIO_ROOT_PASSWORD};

        echo 'Creating buckets...';
        mc mb --ignore-existing rwa/$${BUCKET_AVATARS};
        mc mb --ignore-existing rwa/$${BUCKET_DOCUMENTS};
        mc mb --ignore-existing rwa/$${BUCKET_RESOURCES};
        mc mb --ignore-existing rwa/$${BUCKET_BACKUPS};

        echo 'Setting bucket policies...';
        mc anonymous set download rwa/$${BUCKET_AVATARS};
        mc anonymous set download rwa/$${BUCKET_RESOURCES};

        echo 'Setting lifecycle rules for avatars (keep versions for 30 days)...';
        mc ilm rule add --noncurrent-expire-days 30 rwa/$${BUCKET_AVATARS} || true;

        echo 'MinIO initialization complete!';
        mc admin info rwa;
      "
    networks:
      - rwa-minio

# =============================================================================
# Volumes - 持久化存储
# =============================================================================
volumes:
  minio_data:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${MINIO_DATA_PATH:-/data/minio}

# =============================================================================
# Networks
# =============================================================================
networks:
  rwa-minio:
    driver: bridge
    name: rwa-minio
