mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-07 00:37:28 +08:00
fix(deploy): wire DEEPTUTOR_* proxy env via single JSON-backed exporter
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:<backend_port>. - 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 <noreply@anthropic.com>
This commit is contained in:
42
Dockerfile
42
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}"
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -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"],
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:<backend_port> 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
|
||||
|
||||
Reference in New Issue
Block a user