services:
  app:
    build:
      context: .
    container_name: mailhub-app
    restart: unless-stopped
    env_file:
      - .env
    environment:
      PORT: 3000
      DATA_DIR: /data
      POSTFIX_LOG_FILE: /data/postfix-logs/mail.log
    ports:
      - "127.0.0.1:${APP_PORT:-3025}:3000"
      - "${SUBMISSION_BIND:-0.0.0.0}:25:25"
      - "${SUBMISSION_BIND:-0.0.0.0}:465:465"
      - "${SUBMISSION_BIND:-0.0.0.0}:587:587"
      - "${SUBMISSION_BIND:-0.0.0.0}:${SUBMISSION_ALT_PORT:-2525}:2525"
    volumes:
      - ./data:/data
      - ./certs:/certs:ro
    depends_on:
      postfix:
        condition: service_started
    healthcheck:
      test: ["CMD", "node", "-e", "fetch('http://127.0.0.1:3000/healthz').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 15s

  postfix:
    build:
      context: ./docker/postfix
    container_name: mailhub-postfix
    restart: unless-stopped
    env_file:
      - .env
    environment:
      POSTFIX_LOG_FILE: /var/log/mailhub/mail.log
    hostname: ${MAIL_HOSTNAME:-mailhub.local}
    volumes:
      - ./data/postfix-logs:/var/log/mailhub
    healthcheck:
      test: ["CMD-SHELL", "postfix status >/dev/null 2>&1 || exit 1"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 20s
