# Newses with Docker Compose. Settings go in .env beside this file: copy .env.example.
name: newses

x-app: &app
  image: ${NEWSES_IMAGE:-public.ecr.aws/d5a4a9x3/newses:0.3.14@sha256:c836b22db8052446e694fdfb9c5a32bda00a82ae3a6948a2b612d8330a167816}
  restart: unless-stopped
  environment: &app-environment
    DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}
    APP_SECRET: ${APP_SECRET:?set APP_SECRET in .env}
    LOG_LEVEL: ${LOG_LEVEL:-info}
    COMPAT_TRACE: ${COMPAT_TRACE:-false}
    PUBLIC_URL: ${PUBLIC_URL:-}
    INSTANCE_NAME: ${INSTANCE_NAME:-}
    SOURCE_URL: ${SOURCE_URL:-}
    TRUST_PROXY: ${TRUST_PROXY:-false}
    FETCH_ALLOW_PRIVATE_NETWORKS: ${FETCH_ALLOW_PRIVATE_NETWORKS:-}
    PLUGIN_TRUSTED_HOSTS: ${PLUGIN_TRUSTED_HOSTS:-}
    # The ntfy profile's distributor: the Android app suggests it, and it may be on the LAN.
    PUSH_DISTRIBUTOR_URL: ${NTFY_HOST:+https://${NTFY_HOST:-}}
    PUSH_TRUSTED_HOSTS: ${NTFY_HOST:-}
    # ntfy access tokens to publish with, host=token pairs, where publishing needs an account.
    PUSH_TOKENS: ${PUSH_TOKENS:-}
    FEED_UPDATE_INTERVAL_MINUTES: ${FEED_UPDATE_INTERVAL_MINUTES:-30}
    FEED_PURGE_DAYS: ${FEED_PURGE_DAYS:-180}
    ARCHIVE_ENABLED: ${ARCHIVE_ENABLED:-true}
    FEED_DISABLE_AFTER_FAILURES: ${FEED_DISABLE_AFTER_FAILURES:-20}
    ADMIN_LOGIN: ${ADMIN_LOGIN:-}
    ADMIN_PASSWORD: ${ADMIN_PASSWORD:-}
    OPS_RETENTION_DAYS: ${OPS_RETENTION_DAYS:-90}
    AUDIT_RETENTION_DAYS: ${AUDIT_RETENTION_DAYS:-365}
    OPS_HOST_HOURLY_BUDGET: ${OPS_HOST_HOURLY_BUDGET:-200}
    SMTP_URL: ${SMTP_URL:-}
    MAIL_FROM: ${MAIL_FROM:-}
    ALERT_EMAIL_TO: ${ALERT_EMAIL_TO:-}
    ALERT_EMAIL_FROM: ${ALERT_EMAIL_FROM:-}
    # OpenTelemetry export is off unless an endpoint is set (e.g. http://lgtm:4318).
    OTEL_EXPORTER_OTLP_ENDPOINT: ${OTEL_EXPORTER_OTLP_ENDPOINT:-}
    OTEL_EXPORTER_OTLP_HEADERS: ${OTEL_EXPORTER_OTLP_HEADERS:-}
    OTEL_TRACES_SAMPLER: ${OTEL_TRACES_SAMPLER:-parentbased_always_on}
    OTEL_TRACES_SAMPLER_ARG: ${OTEL_TRACES_SAMPLER_ARG:-1}
    OTEL_METRIC_EXPORT_INTERVAL: ${OTEL_METRIC_EXPORT_INTERVAL:-15000}
    OTEL_RESOURCE_ATTRIBUTES: deployment.environment.name=${DEPLOYMENT_ENVIRONMENT:-self-hosted}

services:
  db:
    image: postgres:18-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - db:/var/lib/postgresql
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U "$${POSTGRES_USER}" -d "$${POSTGRES_DB}"']
      interval: 5s
      timeout: 5s
      retries: 12

  migrate:
    <<: *app
    command: migrate
    restart: 'no'
    depends_on:
      db:
        condition: service_healthy

  server:
    <<: *app
    command: server
    ports:
      - '${HTTP_PORT:-8080}:8080'
    environment:
      <<: *app-environment
      # The apps built by compose.apps.yaml, served at /downloads/; empty until then.
      DOWNLOADS_DIR: /data/downloads
    volumes:
      - blobs:/data/blobs
      - downloads:/data/downloads:ro
    depends_on:
      migrate:
        condition: service_completed_successfully
    healthcheck:
      test: ['CMD', 'wget', '-qO-', 'http://127.0.0.1:8080/healthz']
      interval: 15s
      timeout: 5s
      start_period: 20s
      retries: 3

  worker:
    <<: *app
    command: worker
    volumes:
      - blobs:/data/blobs
    healthcheck:
      test: ['CMD', 'newses', 'worker-health']
      interval: 30s
      timeout: 5s
      start_period: 60s
      retries: 3
    depends_on:
      migrate:
        condition: service_completed_successfully

  # Nightly pg_dump. Enable with COMPOSE_PROFILES=backup in .env.
  backup:
    image: postgres:18-alpine
    profiles: [backup]
    restart: unless-stopped
    environment:
      PGHOST: db
      PGUSER: ${POSTGRES_USER}
      PGPASSWORD: ${POSTGRES_PASSWORD}
      PGDATABASE: ${POSTGRES_DB}
      RETENTION_DAYS: ${BACKUP_RETENTION_DAYS:-14}
    volumes:
      - backups:/backups
    entrypoint:
      - sh
      - -c
      - |
        while true; do
          pg_dump -Fc -f "/backups/newses-$$(date +%Y%m%d-%H%M%S).dump" && echo "Backup written"
          find /backups -name '*.dump' -mtime +"$$RETENTION_DAYS" -delete
          sleep 86400
        done
    depends_on:
      db:
        condition: service_healthy

  # A UnifiedPush distributor for the Android app, at its own hostname (NTFY_HOST, behind your
  # proxy). Anyone may publish to UnifiedPush topics (`up*`), as servers push there, unless
  # NTFY_AUTH_ACCESS=*:up*:deny, when only users given access may, such as this server with its
  # token in PUSH_TOKENS; reading needs a user given access to them. Enable with
  # COMPOSE_PROFILES=ntfy in .env.
  ntfy:
    image: binwiederhier/ntfy:v2.33.1@sha256:29bc952294d58957f6ca6638eca9a653a33555989224edbdf4bb9433ee606497
    profiles: [ntfy]
    restart: unless-stopped
    command: serve
    environment:
      NTFY_BASE_URL: ${NTFY_HOST:+https://${NTFY_HOST:-}}
      NTFY_BEHIND_PROXY: 'true'
      NTFY_CACHE_FILE: /var/lib/ntfy/cache.db
      NTFY_AUTH_FILE: /var/lib/ntfy/auth.db
      NTFY_AUTH_DEFAULT_ACCESS: deny-all
      NTFY_AUTH_ACCESS: '${NTFY_AUTH_ACCESS:-*:up*:wo}'
      NTFY_WEB_ROOT: disable
    ports:
      - '${NTFY_PORT:-8081}:80'
    volumes:
      - ntfy:/var/lib/ntfy
    healthcheck:
      test: ['CMD-SHELL', 'wget -qO- http://127.0.0.1/v1/health | grep -q ''"healthy":true''']
      interval: 60s
      timeout: 10s
      start_period: 40s
      retries: 3

  # Copies the image's Grafana dashboards to lgtm's on each start, so an upgrade refreshes them.
  dashboards:
    <<: *app
    profiles: [observability]
    restart: 'no'
    entrypoint:
      - sh
      - -c
      - find /data/dashboards -mindepth 1 -delete && cp /app/observability/dashboards/*.json /data/dashboards/
    volumes:
      - dashboards:/data/dashboards

  # Grafana with Tempo, Loki, and Prometheus for traces, logs, and metrics, on 127.0.0.1 only.
  # Enable with COMPOSE_PROFILES=observability in .env, and set
  # OTEL_EXPORTER_OTLP_ENDPOINT=http://lgtm:4318 there.
  lgtm:
    image: grafana/otel-lgtm:0.33.1
    profiles: [observability]
    restart: unless-stopped
    ports:
      - '127.0.0.1:${GRAFANA_PORT:-3000}:3000'
    volumes:
      - lgtm:/data
      - dashboards:/otel-lgtm/newses-dashboards:ro
    configs:
      - source: dashboards
        target: /otel-lgtm/grafana/conf/provisioning/dashboards/newses.yaml
    depends_on:
      dashboards:
        condition: service_completed_successfully

configs:
  dashboards:
    content: |
      apiVersion: 1
      providers:
        - name: newses
          folder: newses
          type: file
          options:
            path: /otel-lgtm/newses-dashboards

volumes:
  db:
  blobs:
  downloads:
  backups:
  lgtm:
  dashboards:
  ntfy:
