# ================================================
# Urban Lifeline - All-in-One 后端部署
# 单容器运行所有后端服务
# ================================================

services:
  # ====================== Redis 缓存 ======================
  redis:
    image: redis:7-alpine
    container_name: urban-lifeline-redis
    restart: unless-stopped
    networks:
      - urban-lifeline
    expose:
      - "6379"
    environment:
      TZ: Asia/Shanghai
    command: redis-server --appendonly yes --requirepass ${REDIS_PASSWORD:-redis123}
    volumes:
      - ${REDIS_DATA:-./volumes/redis}:/data
    deploy:
      resources:
        limits:
          memory: ${REDIS_MEMORY_LIMIT:-512M}
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD:-redis123}", "ping"]
      interval: 10s
      timeout: 5s
      retries: 3

  # ====================== 后端服务 ======================
  urban-lifeline-serv:
    image: urban-lifeline-serv:${IMAGE_VERSION:-latest}
    container_name: urban-lifeline-serv
    restart: unless-stopped
    env_file:
      - .env
    networks:
      - urban-lifeline
    # 不对外暴露端口，仅通过 nginx 反向代理访问
    expose:
      - "8080"
    environment:
      TZ: Asia/Shanghai
      SPRING_PROFILES_ACTIVE: ${SPRING_PROFILES_ACTIVE:-prod}
      NACOS_SERVER_ADDR: ${NACOS_SERVER_ADDR:-nacos:8848}
      NACOS_NAMESPACE: ${NACOS_NAMESPACE:-}
      REDIS_HOST: urban-lifeline-redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-redis123}
    depends_on:
      redis:
        condition: service_healthy
    volumes:
      # 配置文件目录
      - ${CONFIG_ROOT:-./volumes/config}:/app/config:ro
      # 日志目录
      - ${LOG_ROOT:-./volumes/logs}:/app/logs
      # 数据目录 (上传文件等)
      - ${DATA_ROOT:-./volumes/data}:/app/data
    deploy:
      resources:
        limits:
          memory: ${MEMORY_LIMIT:-4G}
        reservations:
          memory: ${MEMORY_RESERVATION:-2G}
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 180s

networks:
  urban-lifeline:
    name: urban-lifeline
