x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

services:
  sub2api:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}weishaw/sub2api:${SUB2API_VERSION:-0.1.124}
    ports:
      - '${SUB2API_PORT_OVERRIDE:-8080}:8080'
    environment:
      - AUTO_SETUP=true
      - SERVER_HOST=0.0.0.0
      - SERVER_PORT=8080
      - SERVER_MODE=${SUB2API_SERVER_MODE:-release}
      - RUN_MODE=${SUB2API_RUN_MODE:-standard}
      - TZ=${TZ:-UTC}
      - DATABASE_HOST=postgres
      - DATABASE_PORT=5432
      - DATABASE_USER=${SUB2API_POSTGRES_USER:-sub2api}
      - DATABASE_PASSWORD=${SUB2API_POSTGRES_PASSWORD:-sub2api}
      - DATABASE_DBNAME=${SUB2API_POSTGRES_DB:-sub2api}
      - DATABASE_SSLMODE=disable
      - DATABASE_MAX_OPEN_CONNS=${SUB2API_DATABASE_MAX_OPEN_CONNS:-50}
      - DATABASE_MAX_IDLE_CONNS=${SUB2API_DATABASE_MAX_IDLE_CONNS:-10}
      - DATABASE_CONN_MAX_LIFETIME_MINUTES=${SUB2API_DATABASE_CONN_MAX_LIFETIME_MINUTES:-30}
      - DATABASE_CONN_MAX_IDLE_TIME_MINUTES=${SUB2API_DATABASE_CONN_MAX_IDLE_TIME_MINUTES:-5}
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=${SUB2API_REDIS_PASSWORD:-}
      - REDIS_DB=${SUB2API_REDIS_DB:-0}
      - REDIS_POOL_SIZE=${SUB2API_REDIS_POOL_SIZE:-1024}
      - REDIS_MIN_IDLE_CONNS=${SUB2API_REDIS_MIN_IDLE_CONNS:-10}
      - REDIS_ENABLE_TLS=false
      - ADMIN_EMAIL=${SUB2API_ADMIN_EMAIL:-admin@sub2api.local}
      - ADMIN_PASSWORD=${SUB2API_ADMIN_PASSWORD:-}
      - JWT_SECRET=${SUB2API_JWT_SECRET:-}
      - JWT_EXPIRE_HOUR=${SUB2API_JWT_EXPIRE_HOUR:-24}
      - TOTP_ENCRYPTION_KEY=${SUB2API_TOTP_ENCRYPTION_KEY:-}
      - SECURITY_URL_ALLOWLIST_ENABLED=${SUB2API_SECURITY_URL_ALLOWLIST_ENABLED:-false}
      - SECURITY_URL_ALLOWLIST_ALLOW_INSECURE_HTTP=${SUB2API_SECURITY_URL_ALLOWLIST_ALLOW_INSECURE_HTTP:-false}
      - SECURITY_URL_ALLOWLIST_ALLOW_PRIVATE_HOSTS=${SUB2API_SECURITY_URL_ALLOWLIST_ALLOW_PRIVATE_HOSTS:-false}
      - UPDATE_PROXY_URL=${SUB2API_UPDATE_PROXY_URL:-}
    volumes:
      - sub2api_data:/app/data
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test:
        - CMD
        - wget
        - -q
        - -T
        - '5'
        - -O
        - /dev/null
        - http://localhost:8080/health
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    deploy:
      resources:
        limits:
          cpus: ${SUB2API_CPU_LIMIT:-1.0}
          memory: ${SUB2API_MEMORY_LIMIT:-1024M}
        reservations:
          cpus: ${SUB2API_CPU_RESERVATION:-0.1}
          memory: ${SUB2API_MEMORY_RESERVATION:-256M}

  postgres:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}postgres:${SUB2API_POSTGRES_VERSION:-18-alpine}
    environment:
      - TZ=${TZ:-UTC}
      - POSTGRES_USER=${SUB2API_POSTGRES_USER:-sub2api}
      - POSTGRES_PASSWORD=${SUB2API_POSTGRES_PASSWORD:-sub2api}
      - POSTGRES_DB=${SUB2API_POSTGRES_DB:-sub2api}
      - PGDATA=/var/lib/postgresql/data/pgdata
    volumes:
      - sub2api_postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: [CMD-SHELL, pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 15s
    deploy:
      resources:
        limits:
          cpus: ${SUB2API_POSTGRES_CPU_LIMIT:-1.0}
          memory: ${SUB2API_POSTGRES_MEMORY_LIMIT:-1024M}
        reservations:
          cpus: ${SUB2API_POSTGRES_CPU_RESERVATION:-0.1}
          memory: ${SUB2API_POSTGRES_MEMORY_RESERVATION:-256M}

  redis:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}redis:${SUB2API_REDIS_VERSION:-8-alpine}
    command: >-
      sh -c 'exec redis-server --save 60 1 --appendonly yes --appendfsync everysec
      ${SUB2API_REDIS_PASSWORD:+--requirepass "$SUB2API_REDIS_PASSWORD"}'
    environment:
      - TZ=${TZ:-UTC}
      - REDISCLI_AUTH=${SUB2API_REDIS_PASSWORD:-}
    volumes:
      - sub2api_redis_data:/data
    healthcheck:
      test: [CMD, redis-cli, ping]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 5s
    deploy:
      resources:
        limits:
          cpus: ${SUB2API_REDIS_CPU_LIMIT:-0.5}
          memory: ${SUB2API_REDIS_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${SUB2API_REDIS_CPU_RESERVATION:-0.1}
          memory: ${SUB2API_REDIS_MEMORY_RESERVATION:-128M}

volumes:
  sub2api_data:
  sub2api_postgres_data:
  sub2api_redis_data:
