# Local development reverse proxy
# Unifies API and UI behind a single port.
{
	admin localhost:{$CADDY_ADMIN_PORT:2019}
}

# Recommended:
#   PORT_PREFIX=271  -> proxy 27100, api 27101, ui 27105
# Default main checkout uses the same schema:
#   proxy 9300, api 9301, ui 9305
# Override individual ports with:
#   API_PORT=9301 UI_PORT=9305 PROXY_PORT=9300 CADDY_ADMIN_PORT=2019
#
# Routes:
#   /api/*     -> API server at :API_PORT
#   /oauth/*   -> API server at :API_PORT
#   /mcp       -> API server at :API_PORT
#   /.well-known/* -> API server at :API_PORT
#   /api-doc/* -> API server at :API_PORT
#   /health    -> API server at :API_PORT
#   /*         -> UI dev server at :UI_PORT
#
# SSE/Proxy Notes:
#   - flush_interval -1: disables response buffering (required for SSE)
#   - Server handles its own 5-min connection cycling with disconnecting events
#   - SDK reconnects transparently on disconnecting events (no retry budget cost)
:{$PROXY_PORT:9300} {
	handle /api/* {
		reverse_proxy localhost:{$API_PORT:9301} {
			# Disable response buffering for SSE streaming
			flush_interval -1
		}
	}
	handle /oauth/* {
		reverse_proxy localhost:{$API_PORT:9301}
	}
	handle /mcp {
		reverse_proxy localhost:{$API_PORT:9301}
	}
	handle /.well-known/* {
		reverse_proxy localhost:{$API_PORT:9301}
	}
	handle /api-doc/* {
		reverse_proxy localhost:{$API_PORT:9301}
	}
	handle /health {
		reverse_proxy localhost:{$API_PORT:9301}
	}
	handle {
		reverse_proxy localhost:{$UI_PORT:9305}
	}
}
