services:
  postgres:
    image: postgres:16-alpine
    container_name: open-reception-postgres
    restart: unless-stopped
    user: postgres
    environment:
      POSTGRES_DB_FILE: /run/secrets/postgres_db
      POSTGRES_USER_FILE: /run/secrets/postgres_user
      POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
      POSTGRES_INITDB_ARGS: "--auth-host=scram-sha-256 --auth-local=scram-sha-256"
    secrets:
      - postgres_db
      - postgres_user
      - postgres_password
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./init-db:/docker-entrypoint-initdb.d:ro
    networks:
      - open-reception-internal
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "pg_isready -U $$(cat /run/secrets/postgres_user) -d $$(cat /run/secrets/postgres_db)",
        ]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    security_opt:
      - no-new-privileges:true
    cap_drop:
      - ALL
    cap_add:
      - CHOWN
      - DAC_OVERRIDE
      - FOWNER
      - SETGID
      - SETUID
    tmpfs:
      - /tmp
      - /var/run/postgresql
    read_only: true

  app:
    image: openreception/open-reception:${VERSION:-latest}
    container_name: open-reception-app
    restart: unless-stopped
    user: "1001:1001"
    environment:
      NODE_ENV: production
      SMTP_HOST: /run/secrets/smtp_host
      SMTP_PORT: /run/secrets/smtp_port
      SMTP_SECURE: /run/secrets/smtp_secure
      SMTP_USER: /run/secrets/smtp_user
      SMTP_PASS: /run/secrets/smtp_pass
      SMTP_FROM_NAME: /run/secrets/smtp_from_name
      SMTP_FROM_EMAIL: /run/secrets/smtp_from_email
    secrets:
      - postgres_db
      - postgres_user
      - postgres_password
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - open-reception-internal
    healthcheck:
      test:
        ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://127.0.0.1:3000/api/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    security_opt:
      - no-new-privileges:true
    cap_drop:
      - ALL
    tmpfs:
      - /tmp
    command: >
      sh -c "
        export POSTGRES_DB=$$(cat /run/secrets/postgres_db)
        export POSTGRES_USER=$$(cat /run/secrets/postgres_user)
        export POSTGRES_PASSWORD=$$(cat /run/secrets/postgres_password)
        export DATABASE_URL=\"postgres://$$POSTGRES_USER:$$POSTGRES_PASSWORD@postgres:5432/$$POSTGRES_DB\"
        exec node build/index.js
      "
    read_only: true

  caddy:
    image: caddy:2-alpine
    container_name: open-reception-caddy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    networks:
      - open-reception-internal
    depends_on:
      app:
        condition: service_healthy
    security_opt:
      - no-new-privileges:true
    cap_drop:
      - ALL
    cap_add:
      - NET_BIND_SERVICE

secrets:
  postgres_db:
    file: ./secrets/postgres_db.txt
  postgres_user:
    file: ./secrets/postgres_user.txt
  postgres_password:
    file: ./secrets/postgres_password.txt
  smtp_port:
    file: ./secrets/smtp_port.txt
  smtp_host:
    file: ./secrets/smtp_host.txt
  smtp_secure:
    file: ./secrets/smtp_secure.txt
  smtp_user:
    file: ./secrets/smtp_user.txt
  smtp_pass:
    file: ./secrets/smtp_pass.txt
  smtp_from_name:
    file: ./secrets/smtp_from_name.txt
  smtp_from_email:
    file: ./secrets/smtp_from_email.txt

volumes:
  postgres_data:
    driver: local
  caddy_data:
    driver: local
  caddy_config:
    driver: local

networks:
  open-reception-internal:
    driver: bridge
    internal: false
