Add prefix path to API and WebUI#3007
Conversation
- Add --api-prefix / LIGHTRAG_API_PREFIX for API routes (default: '') - Add --webui-path / LIGHTRAG_WEBUI_PATH for WebUI mount (default: /webui) - Update frontend constants to read from VITE_API_PREFIX and VITE_WEBUI_PREFIX - Add unit tests using test prefixes: unit-test-back/api and unit-test-front/webui - Document new env vars in env.example
The document router already has prefix='/documents' defined internally, so including it with '/test-api/documents' resulted in double '/documents/documents/' path. Now uses api_prefix directly.
Swagger UI static files were mounted at /static/swagger-ui without
the API prefix, causing 404 errors when accessing /docs with a
prefix. Now mounts at {api_prefix}/static/swagger-ui and updates
the HTML references accordingly.
The TestPrefixPathCalculation class only tested Python string concatenation (f-strings), not actual FastAPI route mounting or HTTP behavior. Removed in favor of keeping meaningful tests that verify CLI parsing and environment variable reading.
Replace superficial tests that only checked argument parsing with actual HTTP-level integration tests using TestClient. Tests now verify: - Routes are accessible at prefixed paths (e.g., /test-api/docs) - Unprefixed paths return 404 when prefix is set - Routes work at root when no prefix is set - Document routes don't have duplicate /documents/documents/ path
Add tests to verify WebUI is served at the correct prefixed path: - /test-webui/ returns 200 or 307 redirect - /webui/ returns 404 when custom path is set - Validates WebUI path configuration works correctly
|
If there are better variable naming suggestions or alternative implementations please let me know. |
|
Thanks for submitting this PR! Solving the reverse proxy and sub-path mounting issue is critical for LightRAG as it moves towards more mature, enterprise-grade containerized deployments. The architectural idea of using two environment variables to control the frontend and backend prefixes is very clear. After carefully reviewing the code, I have some architectural suggestions regarding the backend implementation to further optimize this feature: 1. Frontend (WebUI) — Recommended to KeepInjecting the 2. Backend (API) — Recommended to Refactor using
|
- Pass api_prefix to FastAPI's root_path parameter for reverse proxy support - Revert all manual route prefixing (/p() helper, include_router prefix=) - Routes stay at their natural paths (/docs, /query, /health, etc.) - FastAPI injects root_path into ASGI scope so both /prefix/path and /path work - Add tests for: root_path configuration, natural path accessibility, OpenAPI spec servers URL, WebUI mount with and without api_prefix - All 13 path prefix tests pass
- add autouse fixture to clear env vars loaded from developer .env - set minimal ollama defaults so create_app validation passes - prevent flaky failures due to mismatched local api keys or hosts
…upport - add _normalize_path helper to sanitize api_prefix and webui_path inputs - strip trailing slashes and ensure leading slash to prevent Starlette mount errors - treat empty or "/" as default to avoid misconfiguration - prepend ASGI root_path in redirects for correct reverse proxy behavior - update env.example comments to explain build-time VITE prefix requirements - add comprehensive tests for edge cases like trailing slash and slash-only input
- introduce normalizeApiPrefix and normalizeWebuiPrefix for consistent path handling - replace inline env var fallbacks in constants.ts with new utilities - fix vite.config.ts to load env vars via loadEnv instead of import.meta.env - add comprehensive bun test suite covering edge cases and safety invariants
- expand env.example comments to clarify backend/frontend split for reverse proxy deployments - add new lightrag_webui/.env.example template with build-time Vite variable documentation - document VITE_API_PREFIX and VITE_WEBUI_PREFIX in vite-env.d.ts with usage constraints
- expand env.example comments to cover multi-site reverse-proxy deployments - add nginx configuration example and clarify backend/frontend split - rewrite lightrag_webui/.env.example with multi-site use case and build instructions - update vite-env.d.ts inline docs to include multi-site examples and runtime config note
- remove `.env.example` to eliminate duplication with `.env.development` - add reverse-proxy documentation to `.env.development` and `env.local.sample` - keep example values commented out in both files for clarity
- add check_webui_build_prefix function to detect baked prefix mismatches - emit warning with exact rebuild command when prefix disagrees with server config - add comprehensive tests for match, mismatch, and missing build scenarios
- convert module-level router instances to local variables inside `create_*_routes` factories - prevent duplicate route accumulation when factories is invoked multiple times in the same process - remove re-exports from `__init__.py` to avoid exposing stale singleton routers - add detailed comments explaining the factory pattern and rationale
|
Sorry I see you have merged it while I am still working on some changes... At the same time I see you also made some changes. I'll pull and rebase. Give me till tonight to confirm. |
|
PR #3039 resolves the issue where URL prefix changes no longer require recompilation, enabling seamless Docker container and cloud-native deployments. Additionally, it simplifies prefix configuration by removing the |
Description
This PR adds configurable path prefixes for the LightRAG API and WebUI, allowing users to customize URL paths where the API and WebUI are mounted. This is useful for deployments behind reverse proxies or when integrating with existing infrastructure that requires specific path structures.
Related Issues
#2755
Changes Made
1. Configurable API Prefix (
--api-prefix/LIGHTRAG_API_PREFIX)VITE_API_PREFIX/documents/documents/path2. Configurable WebUI Path (
--webui-path/LIGHTRAG_WEBUI_PATH)/webuiVITE_WEBUI_PREFIXfor correct asset paths3. Tests
TestClientthat verify actual HTTP behaviorTestPrefixPathCalculationclass that only tested string concatenationTest Results
Backend Tests (pytest)
Frontend Tests (Bun)