# MikrotikManager + Traefik in one Compose stack (production).
# Docs: README.md (раздел «Прод-развёртывание Docker»)
#
# On server:
#   mkdir -p /opt/mmapp/data /opt/mmapp/state
#   cp deploy/docker-compose.traefik.yml /opt/mmapp/docker-compose.yml
#   cp deploy/env.traefik.example /opt/mmapp/.env   # fill secrets
#   cp deploy/updater/targets.json.example /opt/mmapp/updater/targets.json
#   docker login git.shx.one
#   cd /opt/mmapp && docker compose pull && docker compose up -d
#
# Traffic:
#   Internet → :80/:443 (Traefik) → frontend:3000
#                                    └─ rewrite /api,/health → backend:8000 (internal)
#   Backend is not published on the host — only Traefik exposes 80/443.

services:
  traefik:
    image: traefik:${TRAEFIK_IMAGE_TAG:-v3.7}
    container_name: mmapp-traefik
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    ports:
      - "${TRAEFIK_HTTP_PORT:-80}:80"
      - "${TRAEFIK_HTTPS_PORT:-443}:443"
    environment:
      CF_DNS_API_TOKEN: ${CF_DNS_API_TOKEN:?set CF_DNS_API_TOKEN in .env}
      # Optional if DNS token lacks Zone:Read:
      # CF_ZONE_API_TOKEN: ${CF_ZONE_API_TOKEN:-}
    command:
      - --log.level=${TRAEFIK_LOG_LEVEL:-INFO}
      - --api.dashboard=false
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --providers.docker.network=mmapp
      - --entrypoints.web.address=:80
      - --entrypoints.websecure.address=:443
      - --entrypoints.web.http.redirections.entrypoint.to=websecure
      - --entrypoints.web.http.redirections.entrypoint.scheme=https
      - --certificatesresolvers.letsencrypt.acme.email=${LETSENCRYPT_EMAIL:?set LETSENCRYPT_EMAIL in .env}
      - --certificatesresolvers.letsencrypt.acme.storage=/letsencrypt/acme.json
      - --certificatesresolvers.letsencrypt.acme.dnschallenge=true
      - --certificatesresolvers.letsencrypt.acme.dnschallenge.provider=cloudflare
      - --certificatesresolvers.letsencrypt.acme.dnschallenge.delaybeforecheck=15
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - traefik_letsencrypt:/letsencrypt
    networks:
      - mmapp
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

  backend:
    image: git.shx.one/denozord/mikrotikmanager-backend:${MM_BACKEND_IMAGE_TAG:-latest}
    pull_policy: always
    container_name: mmapp-backend
    restart: unless-stopped
    depends_on:
      - traefik
    # No host ports — frontend reaches backend on the Compose network.
    environment:
      NODE_ENV: production
      PORT: "8000"
      DATABASE_PATH: /app/data/mikrotik.db
      CORS_ORIGIN: ${CORS_ORIGIN:-https://mm.shnt.top}
    volumes:
      - ./data:/app/data
    networks:
      - mmapp
    labels:
      mmapp.updater.managed: "true"
      mmapp.updater.target: backend
      mmapp.updater.image: git.shx.one/denozord/mikrotikmanager-backend:${MM_BACKEND_IMAGE_TAG:-latest}
    healthcheck:
      test:
        [
          "CMD",
          "node",
          "-e",
          "fetch('http://127.0.0.1:8000/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))",
        ]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 15s
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

  frontend:
    image: git.shx.one/denozord/mikrotikmanager-frontend:${MM_FRONTEND_IMAGE_TAG:-latest}
    pull_policy: always
    container_name: mmapp-frontend
    restart: unless-stopped
    depends_on:
      - backend
    # No host ports — only Traefik publishes 80/443.
    environment:
      BACKEND_INTERNAL_URL: http://backend:8000
    networks:
      - mmapp
    labels:
      - mmapp.updater.managed=true
      - mmapp.updater.target=frontend
      - mmapp.updater.image=git.shx.one/denozord/mikrotikmanager-frontend:${MM_FRONTEND_IMAGE_TAG:-latest}
      - traefik.enable=true
      - traefik.docker.network=mmapp
      - traefik.http.routers.mmapp.rule=Host(`${MM_DOMAIN:-mm.shnt.top}`)
      - traefik.http.routers.mmapp.entrypoints=websecure
      - traefik.http.routers.mmapp.tls=true
      - traefik.http.routers.mmapp.tls.certresolver=letsencrypt
      - traefik.http.services.mmapp.loadbalancer.server.port=3000
    healthcheck:
      test:
        [
          "CMD",
          "node",
          "-e",
          "fetch('http://127.0.0.1:3000/dashboard').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))",
        ]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 25s
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

  updater:
    image: git.shx.one/denozord/mikrotikmanager-updater:${MM_UPDATER_IMAGE_TAG:-latest}
    pull_policy: always
    container_name: mmapp-updater
    restart: unless-stopped
    depends_on:
      - frontend
    environment:
      REGISTRY: git.shx.one
      REGISTRY_USERNAME: ${REGISTRY_USERNAME:-}
      REGISTRY_PASSWORD: ${REGISTRY_PASSWORD:-}
      POLL_INTERVAL_SECONDS: ${POLL_INTERVAL_SECONDS:-300}
      HEALTH_TIMEOUT_SECONDS: ${HEALTH_TIMEOUT_SECONDS:-120}
      STOP_TIMEOUT_SECONDS: ${STOP_TIMEOUT_SECONDS:-30}
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./state:/state
      - ./updater/targets.json:/etc/updater/targets.json:ro
    networks:
      - mmapp
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

volumes:
  traefik_letsencrypt:
    name: mmapp_traefik_letsencrypt

networks:
  mmapp:
    name: mmapp
