# ignore: true
# documentation: https://plausible.io/docs/self-hosting
# slogan: "Plausible Analytics is a simple, open-source, lightweight (< 1 KB) and privacy-friendly web analytics alternative to Google Analytics."
# category: analytics
# tags: analytics, privacy, google, alternative
# port: 8000

services:
  plausible:
    image: "ghcr.io/plausible/community-edition:v3.0.1"
    command: 'sh -c "sleep 10 && /entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh run"'
    environment:
      - SERVICE_URL_PLAUSIBLE
      - DATABASE_URL=postgres://${SERVICE_USER_POSTGRES}:${SERVICE_PASSWORD_POSTGRES}@plausible-db:5432/${POSTGRES_DB:-plausible-db}
      - CLICKHOUSE_DATABASE_URL=http://plausible-events-db:8123/plausible_events_db
      - BASE_URL=${SERVICE_URL_PLAUSIBLE}
      - SECRET_KEY_BASE=${SERVICE_BASE64_64_PLAUSIBLE}
      - TOTP_VAULT_KEY=${SERVICE_REALBASE64_32_TOTP}
      - GOOGLE_CLIENT_ID=${GOOGLE_CLIENT_ID}
      - GOOGLE_CLIENT_SECRET=${GOOGLE_CLIENT_SECRET}
      - MAILER_ADAPTER=${MAILER_ADAPTER:-Bamboo.LocalAdapter}
      - MAILER_EMAIL=${MAILER_EMAIL}
      - MAILER_NAME=${MAILER_NAME}
      - SMTP_HOST_ADDR=${SMTP_HOST_ADDR}
      - SMTP_HOST_PORT=${SMTP_HOST_PORT}
      - SMTP_USER_NAME=${SMTP_USER_NAME}
      - SMTP_USER_PWD=${SMTP_USER_PWD}
      - SMTP_HOST_SSL_ENABLED=${SMTP_HOST_SSL_ENABLED}
    depends_on:
      plausible-db:
        condition: service_healthy
      plausible-events-db:
        condition: service_healthy
    healthcheck:
      test:
        [
          "CMD",
          "wget",
          "--no-verbose",
          "--tries=1",
          "--spider",
          "http://127.0.0.1:8000/api/health",
        ]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 45s

  plausible-db:
    image: "postgres:16-alpine"
    volumes:
      - plausible-postgres-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=${POSTGRES_DB:-plausible-db}
      - POSTGRES_USER=${SERVICE_USER_POSTGRES}
      - POSTGRES_PASSWORD=${SERVICE_PASSWORD_POSTGRES}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
      interval: 5s
      timeout: 20s
      retries: 10

  plausible-events-db:
    image: "clickhouse/clickhouse-server:24.12-alpine"
    environment:
      - CLICKHOUSE_SKIP_USER_SETUP=1
    volumes:
      - plausible-events-data:/var/lib/clickhouse
      - type: bind
        source: ./clickhouse/clickhouse-config.xml
        target: /etc/clickhouse-server/config.d/logging.xml
        read_only: true
        content: '<clickhouse><logger><level>warning</level><console>true</console></logger><query_thread_log remove="remove"/><query_log remove="remove"/><text_log remove="remove"/><trace_log remove="remove"/><metric_log remove="remove"/><asynchronous_metric_log remove="remove"/><session_log remove="remove"/><part_log remove="remove"/></clickhouse>'
      - type: bind
        source: ./clickhouse/clickhouse-user-config.xml
        target: /etc/clickhouse-server/users.d/logging.xml
        read_only: true
        content: '<clickhouse><profiles><default><log_queries>0</log_queries><log_query_threads>0</log_query_threads></default></profiles></clickhouse>'
    ulimits:
      nofile:
        soft: 262144
        hard: 262144
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "wget --no-verbose --tries=1 -O - http://127.0.0.1:8123/ping || exit 1",
        ]
      start_period: 30s
