version: "3.7"
services:
  haven:
    image: ghcr.io/havenweb/haven:a0280ce
    container_name: haven
    depends_on:
      - haven-db
    ports:
      - ${APP_PORT}:3000
    volumes:
      - ${APP_DATA_DIR}/data/storage:/storage
    environment:
      - RAILS_ENV=production
      - HAVEN_DB_HOST=haven-db
      - HAVEN_DB_NAME=haven
      - HAVEN_DB_ROLE=haven
      - HAVEN_DB_PASSWORD=${HAVEN_DB_PASSWORD}
      - HAVEN_USER_EMAIL=${HAVEN_USER_EMAIL}
      - HAVEN_USER_PASS=${HAVEN_USER_PASSWORD}
    networks:
      - tipi_main_network
    labels:
      # Main
      traefik.enable: true
      traefik.http.middlewares.haven-web-redirect.redirectscheme.scheme: https
      traefik.http.services.haven.loadbalancer.server.port: 3000
      # Web
      traefik.http.routers.haven-insecure.rule: Host(`${APP_DOMAIN}`)
      traefik.http.routers.haven-insecure.entrypoints: web
      traefik.http.routers.haven-insecure.service: haven
      traefik.http.routers.haven-insecure.middlewares: haven-web-redirect
      # Websecure
      traefik.http.routers.haven.rule: Host(`${APP_DOMAIN}`)
      traefik.http.routers.haven.entrypoints: websecure
      traefik.http.routers.haven.service: haven
      traefik.http.routers.haven.tls.certresolver: myresolver
      # Local domain
      traefik.http.routers.haven-local-insecure.rule: Host(`haven.${LOCAL_DOMAIN}`)
      traefik.http.routers.haven-local-insecure.entrypoints: web
      traefik.http.routers.haven-local-insecure.service: haven
      traefik.http.routers.haven-local-insecure.middlewares: haven-web-redirect
      # Local domain secure
      traefik.http.routers.haven-local.rule: Host(`haven.${LOCAL_DOMAIN}`)
      traefik.http.routers.haven-local.entrypoints: websecure
      traefik.http.routers.haven-local.service: haven
      traefik.http.routers.haven-local.tls: true

  haven-db:
    image: postgres:13.2-alpine
    container_name: haven-db
    command:
      [
        "postgres",
        "-c",
        "max_connections=1000",
        "-c",
        "synchronous_commit=off",
        "-c",
        "fsync=off",
        "-c",
        "full_page_writes=off",
        "-c",
        "max_wal_size=4GB",
        "-c",
        "checkpoint_timeout=30min",
        "-c",
        "wal_level=logical",
      ]
    environment:
      POSTGRES_HOST_AUTH_METHOD: trust
      POSTGRES_USER: haven
    volumes:
      - ${APP_DATA_DIR}/data/db:/var/lib/postgresql/data
    networks:
      - tipi_main_network
