services:
  caddy:
    image: caddy:2-alpine
    container_name: psopeeps-web-caddy
    restart: unless-stopped
    env_file:
      - .env
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - ./site:/srv/site:ro
      - ./caddy-data:/data
      - ./caddy-config:/config
    depends_on:
      - app
    networks:
      - psopeeps-web

  app:
    build:
      context: ./backend
    container_name: psopeeps-web-app
    user: "${PUID}:${PGID}"
    restart: unless-stopped
    env_file:
      - .env
    ports:
      - "5.0.0.21:8080:${APP_PORT:-3000}"
    environment:
      APP_PORT: ${APP_PORT:-8000}
      POSTGRES_HOST: postgres
      POSTGRES_PORT: 5432
      SESSION_COOKIE_NAME: ${SESSION_COOKIE_NAME:-psopeeps_session}
      SESSION_COOKIE_SECURE: ${SESSION_COOKIE_SECURE:-false}
      SESSION_COOKIE_SAMESITE: ${SESSION_COOKIE_SAMESITE:-Lax}
      SESSION_DAYS: ${SESSION_DAYS:-30}
      ACCOUNT_SYNC_ROOT: /account-sync
      ACCOUNT_SYNC_SHARED_SECRET: ${ACCOUNT_SYNC_SHARED_SECRET:-}
      ACCOUNT_LOCK_LEASE_SECONDS: ${ACCOUNT_LOCK_LEASE_SECONDS:-7200}
      PUBLIC_BASE_URL: ${PUBLIC_BASE_URL:-https://psopeeps.online}
      SMTP_HOST: ${SMTP_HOST:-}
      SMTP_PORT: ${SMTP_PORT:-587}
      SMTP_USERNAME: ${SMTP_USERNAME:-}
      SMTP_PASSWORD: ${SMTP_PASSWORD:-}
      SMTP_FROM: ${SMTP_FROM:-}
      SMTP_TLS: ${SMTP_TLS:-starttls}
      EMAIL_DEBUG_SHOW_LINK: ${EMAIL_DEBUG_SHOW_LINK:-false}
      HARDCORE_STATS_EU_URL: ${HARDCORE_STATS_EU_URL:-}
      HARDCORE_STATS_US_URL: ${HARDCORE_STATS_US_URL:-}
    volumes:
      - ../psopeeps_account_sync:/account-sync
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - psopeeps-web

  postgres:
    image: postgres:16-alpine
    container_name: psopeeps-web-postgres
    restart: unless-stopped
    env_file:
      - .env
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - ./postgres-data:/var/lib/postgresql/data
    networks:
      - psopeeps-web
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U \"$${POSTGRES_USER}\" -d \"$${POSTGRES_DB}\""]
      interval: 10s
      timeout: 5s
      retries: 5

networks:
  psopeeps-web:
    name: psopeeps-web
