# DEVELOPMENT DOCKER COMPOSE FILE! DO NOT USE ON PRODUCTION!

x-common-invidious-env: &common-invidious-env
    INVIDIOUS_CONFIG_FILE: "/etc/invidious/config.yml"

x-common: &common-invidious
  image: "git.nadeko.net/fijxu/invidious:latest"
  restart: always
  deploy:
    replicas: 4
  volumes:
    - ./docker/config.yml:/etc/invidious/config.yml:ro
    - valkey_socket:/tmp

x-common-haproxy: &common-haproxy
  hostname: haproxy
  image: haproxy:lts-alpine
  restart: always
  volumes:
    - ./docker/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg

x-common-valkey: &common-valkey
    image: valkey/valkey:7.2-alpine
    hostname: valkey
    restart: unless-stopped
    volumes:
      - ./docker/valkey.conf:/usr/local/etc/valkey/valkey.conf
      - valkey_socket:/tmp
    command: "valkey-server /usr/local/etc/valkey/valkey.conf"

services:

  # INVIDIOUS SECTION #
  invidious:
    <<: *common-invidious
    environment:
      <<: *common-invidious-env
    networks:
      - invidious
    depends_on:
      - valkey
      - pgbouncer
      - haproxy

  # HAPROXY SECTION #
  haproxy:
    <<: *common-haproxy
    ports:
      # Port to expose invidious
      - "127.0.0.1:11101:8001"
    networks:
      - invidious

  valkey:
    <<: *common-valkey
    networks:
      - invidious

  postgres:
    image: docker.io/library/postgres:17
    restart: unless-stopped
    volumes:
      - /tmp/postgresdata:/var/lib/postgresql/data
      - ./config/sql:/config/sql
      - ./docker/init-invidious-db.sh:/docker-entrypoint-initdb.d/init-invidious-db.sh
    environment:
      POSTGRES_DB: invidious
      POSTGRES_USER: kemal
      POSTGRES_PASSWORD: kemal
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
    networks:
      - invidious

  # Pgbouncer to keep connections to the database open
  pgbouncer:
    image: edoburu/pgbouncer
    restart: unless-stopped
    environment:
      - POOL_MODE=transaction
      # Everything is being done locally
      # https://www.pgbouncer.org/config.html
      - AUTH_TYPE=scram-sha-256
      - DB_HOST=postgres
      - DB_USER=kemal
      - DB_PASSWORD=kemal
      - DB_NAME=invidious
      - VERBOSE=3
      - LISTEN_PORT=6432
    networks:
      - invidious
    depends_on:
      postgres:
        condition: service_healthy

networks:
  invidious:
    name: invidious

volumes:
  valkey_socket:
  postgresdata:
