# ─────────────────────────────────────────────
# HAProxy configuration for SSO PostgreSQL HA
# Routes: 5432 (write) / 5433 (read)
# ─────────────────────────────────────────────

global
  log stdout format raw local0
  maxconn 4096

defaults
  log global
  mode tcp
  timeout connect 5s
  timeout client 60s
  timeout server 60s

# ── Write endpoint (primary) ─────────────────
frontend postgres_write
  bind *:5432
  default_backend postgres_primary

backend postgres_primary
  option httpchk GET /primary
  http-check expect status 200
  server pg1 postgres1:5432 check port 8008 inter 5s rise 2 fall 3
  server pg2 postgres2:5432 check port 8008 inter 5s rise 2 fall 3
  server pg3 postgres3:5432 check port 8008 inter 5s rise 2 fall 3

# ── Read endpoint (replicas) ───────────────
frontend postgres_read
  bind *:5433
  default_backend postgres_replica

backend postgres_replica
  option httpchk GET /replica
  http-check expect status 200
  server pg1 postgres1:5432 check port 8008 inter 5s rise 2 fall 3
  server pg2 postgres2:5432 check port 8008 inter 5s rise 2 fall 3
  server pg3 postgres3:5432 check port 8008 inter 5s rise 2 fall 3

# ── Stats UI ─────────────────────────────────
listen stats
  bind *:8404
  mode http
  stats enable
  stats uri /stats
  stats refresh 30s
