# XSI deployment: API, consoles, mail, PostgreSQL, Redis and daily backups.
#
# Download the template and generate credentials.
#
#   curl -fsSL https://xsirouter.com/deploy/docker-compose.yml -o docker-compose.yml
#   sed -i "s/CHANGE_ME_POSTGRES_PASSWORD/$(openssl rand -hex 24)/g" docker-compose.yml
#   sed -i "s/CHANGE_ME_ROOT_KEY/sk-Xvs$(openssl rand -hex 24 | cut -c1-45)/" docker-compose.yml
#
# Then replace the example domains, admin@example.com and the SMTP settings.
# Write a literal $ in any value as $$.
name: xsi

# Shared API and console settings. Use bare hostnames for site addresses.
x-site: &site
  XAI_SVC: xsi.example.com
  XAI_API: api.example.com
  XAI_ADMIN: admin.example.com
  XAI_MANAGE: manage.example.com
  XAI_DOCS: docs.example.com

x-tz: &tz Asia/Shanghai

services:
  xsirouter-xsi:
    container_name: xsirouter-xsi
    image: proxyxai/xsi
    pull_policy: always
    restart: always
    stop_grace_period: 60s
    ulimits:
      nofile:
        soft: 1048576
        hard: 1048576
    depends_on:
      xsirouter-redis:
        condition: service_started
      xsirouter-postgres:
        condition: service_healthy
    ports:
      # 127.0.0.1 serves a reverse proxy on this host.
      - "127.0.0.1:5443:3443"
    environment:
      <<: *site
      TZ: *tz
      PORT: "3443"
      POSTGRES_DSN: "user=postgres password=CHANGE_ME_POSTGRES_PASSWORD host=xsirouter-postgres dbname=postgres port=5432 sslmode=disable TimeZone=Asia/Shanghai"
      REDIS_ADDR: xsirouter-redis:6379
      MAIL_REDIS_DB: "1"
      # root account: name::email::key. The key is sk-Xvs plus 45 characters.
      INIT_USERS: "root::admin@example.com::CHANGE_ME_ROOT_KEY"
      # Recipient of platform notifications.
      XAI_MAIL: admin@example.com
      # Proxies whose X-Forwarded-For is trusted for the client IP.
      TRUSTED_PROXY_CIDRS: "127.0.0.1/32,::1/128,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16"
      # Public email signup; configure SMTP below before enabling it.
      SIGN_ENABLE: "true"
      XAI_CREDIT: "0"
      USER_MIN_BALANCE: "2"
      MODEL_FAILOVER_THRESHOLD: "2"
      # Mail is delivered by xsirouter-mail; tenants may set their own SMTP.
      EMAIL_SMTP: ""
      EMAIL_AUTH: ""
      EMAIL_PASS: ""
    networks:
      - network

  xsirouter-pages:
    container_name: xsirouter-pages
    image: proxyxai/xsi-pages
    pull_policy: always
    restart: always
    ports:
      - "127.0.0.1:5080:8080" # Admin Console
      - "127.0.0.1:5081:8081" # Manage Console
    environment:
      <<: *site
      TZ: *tz
    networks:
      - network

  xsirouter-mail:
    container_name: xsirouter-mail
    image: proxyxai/mail
    pull_policy: always
    restart: always
    stop_grace_period: 40s
    depends_on:
      xsirouter-redis:
        condition: service_started
    environment:
      TZ: *tz
      REDIS_ADDR: xsirouter-redis:6379
      REDIS_DB: "1"
      REDIS_POP_TIMEOUT: "5"
      EMAIL_WORKERS: "1"
      # Your SMTP service. EMAIL_TLS=true is implicit TLS, usually port 465.
      EMAIL_SMTP: smtp.example.com
      EMAIL_PORT: "465"
      EMAIL_TLS: "true"
      EMAIL_AUTH: notifications@example.com
      EMAIL_PASS: CHANGE_ME_SMTP_PASSWORD
    networks:
      - network

  xsirouter-redis:
    container_name: xsirouter-redis
    image: redis:alpine
    pull_policy: always
    restart: always
    volumes:
      - redis_data:/data
    environment:
      TZ: *tz
    networks:
      - network

  xsirouter-postgres:
    container_name: xsirouter-postgres
    image: postgres:19
    pull_policy: always
    restart: always
    user: postgres
    environment:
      TZ: *tz
      PGTZ: *tz
      POSTGRES_USER: postgres
      POSTGRES_DB: postgres
      POSTGRES_PASSWORD: CHANGE_ME_POSTGRES_PASSWORD
    volumes:
      - postgres_data:/var/lib/postgresql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -q -d postgres -U postgres"]
      interval: 2s
      timeout: 5s
      retries: 3
    networks:
      - network

  # Backups run once at start, then daily, into ./backups and are kept for
  # BACKUP_RETENTION_DAYS whole days. Copy them off this host as well.
  xsirouter-redis-backup:
    container_name: xsirouter-redis-backup
    image: redis:alpine
    pull_policy: always
    restart: always
    depends_on:
      xsirouter-redis:
        condition: service_started
    environment:
      TZ: *tz
      BACKUP_RETENTION_DAYS: "3"
    volumes:
      - ./backups/redis:/backups
    entrypoint:
      - sh
      - -ceu
      - |
        umask 077
        mkdir -p /backups
        while true; do
          ts=$$(date +%F_%H-%M-%S)
          tmp="/backups/$${ts}.rdb.tmp"
          out="/backups/$${ts}.rdb"
          redis-cli -h xsirouter-redis --rdb "$${tmp}"
          mv "$${tmp}" "$${out}"
          find /backups -name "*.rdb" -mtime +$${BACKUP_RETENTION_DAYS:-3} -delete
          sleep 86400
        done
    networks:
      - network

  xsirouter-postgres-backup:
    container_name: xsirouter-postgres-backup
    image: postgres:19
    pull_policy: always
    restart: always
    environment:
      TZ: *tz
      PGPASSWORD: CHANGE_ME_POSTGRES_PASSWORD
      BACKUP_RETENTION_DAYS: "3"
    volumes:
      - ./backups/postgres:/backups
    depends_on:
      xsirouter-postgres:
        condition: service_healthy
    networks:
      - network
    entrypoint:
      - bash
      - -o
      - pipefail
      - -ceu
      - |
        umask 077
        mkdir -p /backups
        while true; do
          ts=$$(date +%F_%H-%M-%S)
          tmp="/backups/$${ts}.sql.gz.tmp"
          out="/backups/$${ts}.sql.gz"
          pg_dump -h xsirouter-postgres -U postgres -d postgres --clean --if-exists --no-owner --no-privileges | gzip -9 > "$${tmp}"
          mv "$${tmp}" "$${out}"
          find /backups -name "*.sql.gz" -mtime +$${BACKUP_RETENTION_DAYS:-3} -delete
          sleep 86400
        done

volumes:
  postgres_data:
  redis_data:

networks:
  network:
    driver: bridge
