version: "3.8"

services:
  db:
    image: postgres:17-alpine
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: posse_party
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5
    volumes:
      - db_data:/var/lib/postgresql/data
    networks:
      - posseparty-network
    deploy:
      placement:
        constraints:
          - node.labels.type == nas
      restart_policy:
        condition: on-failure

  migrate:
    image: ghcr.io/searlsco/posse_party:latest
    environment:
      DATABASE_URL: postgres://postgres:${POSTGRES_PASSWORD}@db:5432/posse_party
      RAILS_ENV: production
      SECRET_KEY_BASE: ${SECRET_KEY_BASE}
    command: ["./script/release"]
    networks:
      - posseparty-network
    deploy:
      placement:
        constraints:
          - node.labels.type == nas
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 10

  web:
    image: ghcr.io/searlsco/posse_party:latest
    environment:
      DATABASE_URL: postgres://postgres:${POSTGRES_PASSWORD}@db:5432/posse_party
      RAILS_ENV: production
      APP_HOST: ${APP_HOST:-posseparty.bendtstudio.com}
      SECRET_KEY_BASE: ${SECRET_KEY_BASE}
      FORCE_SSL: "false"
    command: ["./script/server"]
    networks:
      - posseparty-network
      - dokploy-network
    deploy:
      placement:
        constraints:
          - node.labels.type == nas
      restart_policy:
        condition: on-failure
        delay: 30s
        max_attempts: 10
      labels:
        - traefik.enable=true
        - traefik.http.routers.posseparty.rule=Host(`${APP_HOST:-posseparty.bendtstudio.com}`)
        - traefik.http.routers.posseparty.entrypoints=web
        - traefik.http.services.posseparty.loadbalancer.server.port=3000
        - traefik.http.routers.posseparty.middlewares=redirect-to-https@file
        - traefik.http.routers.posseparty-secure.rule=Host(`${APP_HOST:-posseparty.bendtstudio.com}`)
        - traefik.http.routers.posseparty-secure.entrypoints=websecure
        - traefik.http.routers.posseparty-secure.tls.certresolver=letsencrypt

  worker:
    image: ghcr.io/searlsco/posse_party:latest
    environment:
      DATABASE_URL: postgres://postgres:${POSTGRES_PASSWORD}@db:5432/posse_party
      RAILS_ENV: production
      APP_HOST: ${APP_HOST:-posseparty.bendtstudio.com}
      SECRET_KEY_BASE: ${SECRET_KEY_BASE}
    command: ["./script/worker"]
    networks:
      - posseparty-network
    deploy:
      placement:
        constraints:
          - node.labels.type == nas
      restart_policy:
        condition: on-failure
        delay: 30s
        max_attempts: 10

volumes:
  db_data:

networks:
  posseparty-network:
    driver: overlay
  dokploy-network:
    external: true
