name: jiaowu-demo

x-jiaowu-image: &jiaowu-image
  image: "${JIAOWU_IMAGE:-jiaowu:local}"
  build:
    context: .
    dockerfile: Dockerfile

x-jiaowu-environment: &jiaowu-environment
  ASPNETCORE_ENVIRONMENT: Production
  ASPNETCORE_HTTP_PORTS: "8080"
  Database__Provider: MySql
  Database__ApplyMigrationsOnStartup: "false"
  Database__CommandTimeoutSeconds: "30"
  ConnectionStrings__MySql: "Server=mysql;Port=3306;Database=${MYSQL_DATABASE:-jiaowu_demo};User=${MYSQL_USER:-jiaowu};Password=${MYSQL_PASSWORD:?请在 .env.docker 中设置 MYSQL_PASSWORD};SslMode=Disabled;"
  ConnectionStrings__Redis: "redis:6379,abortConnect=false"
  Cache__Enabled: "true"
  Cache__KeyPrefix: "jiaowu:v1"
  BackgroundJobs__Transport: RabbitMq
  BackgroundJobs__AutomaticScheduleConcurrency: "${BACKGROUND_JOB_AUTOMATIC_SCHEDULE_CONCURRENCY:-1}"
  BackgroundJobs__SchedulePublishConcurrency: "${BACKGROUND_JOB_SCHEDULE_PUBLISH_CONCURRENCY:-1}"
  BackgroundJobs__MakeupExamAutoConcurrency: "${BACKGROUND_JOB_MAKEUP_EXAM_AUTO_CONCURRENCY:-1}"
  BackgroundJobs__ExamArrangementConcurrency: "${BACKGROUND_JOB_EXAM_ARRANGEMENT_CONCURRENCY:-1}"
  RabbitMq__HostName: rabbitmq
  RabbitMq__Port: "5672"
  RabbitMq__UserName: "${RABBITMQ_USER:-jiaowu}"
  RabbitMq__Password: "${RABBITMQ_PASSWORD:?请在 .env.docker 中设置 RABBITMQ_PASSWORD}"
  RabbitMq__VirtualHost: "/"
  RabbitMq__UseTls: "false"
  Jwt__Issuer: Jiaowu.Api
  Jwt__Audience: Jiaowu.Web
  Jwt__Key: "${JWT_KEY:?请在 .env.docker 中设置 JWT_KEY}"
  Jwt__AccessTokenMinutes: "10"
  Jwt__WebIdleMinutes: "30"
  Jwt__AppIdleMinutes: "4320"
  AllowedHosts: "${ALLOWED_HOSTS:-localhost}"
  Cors__Origins__0: "${CORS_ORIGIN:-http://localhost:8080}"
  OfficialDocuments__PublicBaseUrl: "${OFFICIAL_DOCUMENTS_PUBLIC_BASE_URL:-http://localhost:8080}"
  SeedAdmin__UserName: "${SEED_ADMIN_USERNAME:-}"
  SeedAdmin__Password: "${SEED_ADMIN_PASSWORD:-}"
  SeedAdmin__DisplayName: "${SEED_ADMIN_DISPLAY_NAME:-}"

x-json-logging: &json-logging
  driver: json-file
  options:
    max-size: "10m"
    max-file: "3"

services:
  rabbitmq:
    image: rabbitmq:4.2-management-alpine
    restart: unless-stopped
    environment:
      RABBITMQ_DEFAULT_USER: "${RABBITMQ_USER:-jiaowu}"
      RABBITMQ_DEFAULT_PASS: "${RABBITMQ_PASSWORD:?请在 .env.docker 中设置 RABBITMQ_PASSWORD}"
    healthcheck:
      test:
        - CMD
        - rabbitmq-diagnostics
        - -q
        - ping
      interval: 10s
      timeout: 5s
      retries: 12
      start_period: 20s
    volumes:
      - rabbitmq-data:/var/lib/rabbitmq
    logging: *json-logging

  redis:
    image: redis:8.8-alpine
    restart: unless-stopped
    command:
      - redis-server
      - --save
      - ""
      - --appendonly
      - "no"
      - --maxmemory
      - 256mb
      - --maxmemory-policy
      - allkeys-lfu
    healthcheck:
      test:
        - CMD
        - redis-cli
        - ping
      interval: 5s
      timeout: 3s
      retries: 10
    logging: *json-logging

  mysql:
    image: mysql:8.4
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: "${MYSQL_DATABASE:-jiaowu_demo}"
      MYSQL_USER: "${MYSQL_USER:-jiaowu}"
      MYSQL_PASSWORD: "${MYSQL_PASSWORD:?请在 .env.docker 中设置 MYSQL_PASSWORD}"
      MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD:?请在 .env.docker 中设置 MYSQL_ROOT_PASSWORD}"
    command:
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_0900_ai_ci
    healthcheck:
      test:
        - CMD-SHELL
        - MYSQL_PWD="$${MYSQL_ROOT_PASSWORD}" mysqladmin ping --host=127.0.0.1 --user=root --silent
      interval: 5s
      timeout: 5s
      retries: 20
      start_period: 30s
    volumes:
      - mysql-data:/var/lib/mysql
    logging: *json-logging

  migrate:
    <<: *jiaowu-image
    command:
      - --migrate-only
    environment: *jiaowu-environment
    depends_on:
      mysql:
        condition: service_healthy
    restart: "no"
    init: true
    logging: *json-logging

  app:
    <<: *jiaowu-image
    environment: *jiaowu-environment
    depends_on:
      rabbitmq:
        condition: service_healthy
      redis:
        condition: service_started
      mysql:
        condition: service_healthy
      migrate:
        condition: service_completed_successfully
    ports:
      - "${JIAOWU_PORT:-8080}:8080"
    restart: unless-stopped
    init: true
    volumes:
      - backup-data:/var/lib/jiaowu/backups
    logging: *json-logging

  # 工具型一次性服务：普通 docker compose up 不会执行它。
  demo-data:
    <<: *jiaowu-image
    profiles:
      - tools
    command:
      - --seed-demo-data
      - --confirm-production-demo-data
    environment: *jiaowu-environment
    depends_on:
      mysql:
        condition: service_healthy
      migrate:
        condition: service_completed_successfully
    restart: "no"
    init: true
    logging: *json-logging

volumes:
  mysql-data:
  rabbitmq-data:
  backup-data:
