From dfa0c9b1ea62fd169085c77cee55ed1d119ba4e0 Mon Sep 17 00:00:00 2001 From: "Bingxi Zhao (Frank)" <13051345080@163.com> Date: Sat, 20 Jun 2026 21:12:40 +0800 Subject: [PATCH] fix(deploy): wire DEEPTUTOR_* proxy env via single JSON-backed exporter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The PR's relative-path + proxy.ts rewrite design depends on two env vars (DEEPTUTOR_API_BASE_URL, DEEPTUTOR_AUTH_ENABLED) read server-side by the Next.js middleware. As submitted they were only set by the Docker entrypoint, and the auth one read a non-existent auth.json key — so auth enforcement was silently disabled and the PyPI `deeptutor start` path was unwired. - Export both vars from render_environment() (the single JSON-backed source of truth), so the Docker entrypoint AND the `deeptutor start` launcher get them consistently. DEEPTUTOR_API_BASE_URL falls back to localhost:. - launcher.py: set both in common_env using the resolved backend_url, so they track port-conflict reassignment. - Dockerfile: drop the duplicated, buggy per-var heredocs (one read the wrong key `auth_enabled` instead of `enabled`, leaving the proxy auth gate always off); the export_runtime_settings_to_env eval now provides both. Add them to the env-unset list for hygiene. - Update the two placeholder tests to assert the new proxy-based design instead of the removed build-time sed mechanism; add DEEPTUTOR_* coverage in test_runtime_settings. Co-Authored-By: Claude Opus 4.8 --- Dockerfile | 42 +++++-------------- deeptutor/runtime/launcher.py | 7 ++++ deeptutor/services/config/runtime_settings.py | 14 +++++++ tests/scripts/test_docker_compose.py | 29 +++++++++---- .../services/config/test_runtime_settings.py | 7 ++++ 5 files changed, 61 insertions(+), 38 deletions(-) diff --git a/Dockerfile b/Dockerfile index a73a48f41..bd6623cbd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -303,7 +303,9 @@ for key in \ POCKETBASE_PORT \ POCKETBASE_EXTERNAL_URL \ POCKETBASE_ADMIN_EMAIL \ - POCKETBASE_ADMIN_PASSWORD; do + POCKETBASE_ADMIN_PASSWORD \ + DEEPTUTOR_API_BASE_URL \ + DEEPTUTOR_AUTH_ENABLED; do unset "$key" done @@ -333,36 +335,14 @@ PY export BACKEND_PORT=${BACKEND_PORT:-8001} export FRONTEND_PORT=${FRONTEND_PORT:-3782} -# Export DEEPTUTOR_API_BASE_URL for proxy.ts (rewrites /api/* and /ws/* to the -# configured backend at request time). Precedence: in-network -# `next_public_api_base` first, then `next_public_api_base_external`, then -# localhost:${BACKEND_PORT} (dev default). -DEEPTUTOR_API_BASE_URL=$(python - <<'PY' -import json, os -from pathlib import Path -try: - s = json.loads(Path("/app/data/user/settings/system.json").read_text(encoding="utf-8")) - base = s.get("next_public_api_base") or s.get("next_public_api_base_external") or f"http://localhost:{os.environ.get('BACKEND_PORT', '8001')}" - print(base) -except Exception: - print(f"http://localhost:{os.environ.get('BACKEND_PORT', '8001')}") -PY -) -export DEEPTUTOR_API_BASE_URL -echo "📌 API Base URL (proxy): ${DEEPTUTOR_API_BASE_URL}" - -DEEPTUTOR_AUTH_ENABLED=$(python - <<'PY' -import json -from pathlib import Path -try: - s = json.loads(Path("/app/data/user/settings/auth.json").read_text(encoding="utf-8")) - val = s.get("auth_enabled", False) - print("true" if val else "false") -except Exception: - print("false") -PY -) -export DEEPTUTOR_AUTH_ENABLED +# DEEPTUTOR_API_BASE_URL and DEEPTUTOR_AUTH_ENABLED are exported by the +# export_runtime_settings_to_env eval above (see render_environment in +# deeptutor/services/config/runtime_settings.py). web/proxy.ts reads them at +# request time to rewrite /api/* and /ws/* to the backend and to gate the login +# redirect. Keeping them in the single JSON-backed exporter means the Docker and +# `deeptutor start` paths stay in sync. +echo "📌 API Base URL (proxy): ${DEEPTUTOR_API_BASE_URL:-http://localhost:${BACKEND_PORT}}" +echo "📌 Auth enabled: ${DEEPTUTOR_AUTH_ENABLED:-false}" echo "📌 Backend Port: ${BACKEND_PORT}" echo "📌 Frontend Port: ${FRONTEND_PORT}" diff --git a/deeptutor/runtime/launcher.py b/deeptutor/runtime/launcher.py index 15e245c0c..c0bd6e4a2 100644 --- a/deeptutor/runtime/launcher.py +++ b/deeptutor/runtime/launcher.py @@ -817,6 +817,13 @@ def start(home: str | Path | None = None) -> None: common_env["HOSTNAME"] = "0.0.0.0" common_env["NEXT_PUBLIC_API_BASE"] = api_base common_env["NEXT_PUBLIC_AUTH_ENABLED"] = "true" if auth_enabled else "false" + # The Next.js middleware (web/proxy.ts) runs in the frontend's Node runtime + # and reads these at request time to forward /api/* and /ws/* to the backend + # and to gate the login redirect. The browser uses relative paths, so the + # frontend server reaches the backend on localhost at the resolved port — + # use backend_url (not api_base, which may be an external browser URL). + common_env["DEEPTUTOR_API_BASE_URL"] = backend_url + common_env["DEEPTUTOR_AUTH_ENABLED"] = "true" if auth_enabled else "false" common_env["PYTHONUNBUFFERED"] = "1" common_env["PYTHONIOENCODING"] = "utf-8:replace" diff --git a/deeptutor/services/config/runtime_settings.py b/deeptutor/services/config/runtime_settings.py index 32e7cbecf..555aac201 100644 --- a/deeptutor/services/config/runtime_settings.py +++ b/deeptutor/services/config/runtime_settings.py @@ -498,6 +498,20 @@ class RuntimeSettingsService: "AUTH_TOKEN_EXPIRE_HOURS": str(auth["token_expire_hours"]), "AUTH_COOKIE_SECURE": _bool_env(auth["cookie_secure"]), "NEXT_PUBLIC_AUTH_ENABLED": _bool_env(auth["enabled"]), + # Consumed server-side by the Next.js middleware (web/proxy.ts) at + # request time — NOT inlined into the browser bundle. The proxy + # rewrites /api/* and /ws/* to DEEPTUTOR_API_BASE_URL and uses + # DEEPTUTOR_AUTH_ENABLED to gate the login redirect. The launcher and + # the Docker entrypoint both export these through render_environment, + # so the two deployment paths stay in sync. DEEPTUTOR_API_BASE_URL is + # the address the frontend *server* uses to reach the backend; the + # browser itself only ever talks to the frontend origin. + "DEEPTUTOR_API_BASE_URL": ( + system["next_public_api_base"] + or system["next_public_api_base_external"] + or f"http://localhost:{system['backend_port']}" + ), + "DEEPTUTOR_AUTH_ENABLED": _bool_env(auth["enabled"]), "POCKETBASE_URL": integrations["pocketbase_url"], "POCKETBASE_PORT": str(integrations["pocketbase_port"]), "POCKETBASE_EXTERNAL_URL": integrations["pocketbase_external_url"], diff --git a/tests/scripts/test_docker_compose.py b/tests/scripts/test_docker_compose.py index 0a1f55a2e..ddca91ef8 100644 --- a/tests/scripts/test_docker_compose.py +++ b/tests/scripts/test_docker_compose.py @@ -73,17 +73,32 @@ def test_compose_files_do_not_consume_legacy_env_names() -> None: assert "DEEPTUTOR_DOCKER_BACKEND_PORT" in content -def test_dockerfile_uses_runtime_auth_placeholder() -> None: +def test_dockerfile_is_json_driven_without_bundle_sed() -> None: + """The image no longer rewrites the built bundle at startup (the runtime + ``sed -i`` broke under a read-only rootfs). URL/auth knowledge is JSON-driven: + the entrypoint re-exports runtime settings from data/user/settings/*.json + (including DEEPTUTOR_API_BASE_URL / DEEPTUTOR_AUTH_ENABLED) and web/proxy.ts + forwards /api/* and /ws/* to the backend at request time.""" root = Path(__file__).resolve().parents[2] content = (root / "Dockerfile").read_text(encoding="utf-8") - assert "__NEXT_PUBLIC_API_BASE_PLACEHOLDER__" in content - assert "__NEXT_PUBLIC_AUTH_ENABLED_PLACEHOLDER__" in content + # The build-time placeholder + runtime bundle sed mechanism is gone. + assert "__NEXT_PUBLIC_API_BASE_PLACEHOLDER__" not in content + assert "__NEXT_PUBLIC_AUTH_ENABLED_PLACEHOLDER__" not in content + # Still JSON-driven: stale runtime env names are ignored and re-exported + # from the settings JSON on every start. assert "DEEPTUTOR_IGNORE_PROCESS_ENV_OVERRIDES=1" in content assert 'unset "$key"' in content + assert "export_runtime_settings_to_env" in content -def test_frontend_placeholder_validation_is_safe_for_runtime_replacement() -> None: +def test_frontend_api_is_url_agnostic_passthrough() -> None: + """web/lib/api.ts no longer carries a build-time API base or a placeholder + token; apiUrl/wsUrl are pass-throughs and the Next.js middleware + (web/proxy.ts) performs the forwarding at request time.""" root = Path(__file__).resolve().parents[2] - content = (root / "web" / "lib" / "api.ts").read_text(encoding="utf-8") - assert 'const API_BASE_PLACEHOLDER = "__NEXT_PUBLIC_API_BASE_PLACEHOLDER__"' not in content - assert "NEXT_PUBLIC_API_BASE_PLACEHOLDER" in content + api_ts = (root / "web" / "lib" / "api.ts").read_text(encoding="utf-8") + assert "NEXT_PUBLIC_API_BASE_PLACEHOLDER" not in api_ts + assert "process.env.NEXT_PUBLIC_API_BASE" not in api_ts + proxy_ts = (root / "web" / "proxy.ts").read_text(encoding="utf-8") + assert "DEEPTUTOR_API_BASE_URL" in proxy_ts + assert "NextResponse.rewrite" in proxy_ts diff --git a/tests/services/config/test_runtime_settings.py b/tests/services/config/test_runtime_settings.py index d0dcc8879..d9e38daf8 100644 --- a/tests/services/config/test_runtime_settings.py +++ b/tests/services/config/test_runtime_settings.py @@ -105,6 +105,13 @@ def test_render_environment_uses_json_backed_runtime_names(monkeypatch, tmp_path assert env["DISABLE_SSL_VERIFY"] == "true" assert env["AUTH_ENABLED"] == "true" assert env["NEXT_PUBLIC_AUTH_ENABLED"] == "true" + # Server-side proxy contract consumed by web/proxy.ts (the Next.js + # middleware). DEEPTUTOR_AUTH_ENABLED gates the login redirect; + # DEEPTUTOR_API_BASE_URL is where the frontend server reaches the backend + # (falls back to localhost: when no in-network / external base + # is configured). + assert env["DEEPTUTOR_AUTH_ENABLED"] == "true" + assert env["DEEPTUTOR_API_BASE_URL"] == "http://localhost:8010" assert env["AUTH_TOKEN_EXPIRE_HOURS"] == "12" assert env["POCKETBASE_URL"] == "http://pocketbase:8090" assert "AUTH_SECRET" not in env