From ffa5c454b3607cae4496946d0f59ecb7121d01c2 Mon Sep 17 00:00:00 2001 From: zj1123581321 Date: Wed, 23 Sep 2026 16:24:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(lifespan):=20PUBLIC=5FBASE=5FURL=20?= =?UTF-8?q?=E7=95=99=E7=A9=BA=E6=97=B6=E5=90=AF=E5=8A=A8=E6=89=93=20WARNIN?= =?UTF-8?q?G=20=E6=8F=90=E7=A4=BA=20(#15)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Agent-Executor: agy Agent-Model: gemini-3.8-flash-high Agent-Effort: unknown Dispatch-Id: dlg-20260923-082221-6ccc70 Task-Id: MediaResolverAPI-20260923-02 --- app/main.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/main.py b/app/main.py index bfb3517..be4c13d 100644 --- a/app/main.py +++ b/app/main.py @@ -60,6 +60,10 @@ async def lifespan(app: FastAPI): # Startup setup_logging() logger.info(f"Starting {settings.APP_NAME} v{settings.APP_VERSION}") + if not (settings.PUBLIC_BASE_URL or "").strip(): + logger.warning( + "PUBLIC_BASE_URL 未设置,data.video_url 将按请求 Host 推导,跨机调用会拿到不可用地址" + ) init_db() logger.info("Database initialized") _cleanup_expired_data() From 8cdc20a305995d0dd00b6f829dd0f4d2273a3f28 Mon Sep 17 00:00:00 2001 From: zj1123581321 Date: Wed, 23 Sep 2026 16:24:29 +0800 Subject: [PATCH 2/2] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=20PUBLIC=5FBASE?= =?UTF-8?q?=5FURL=20=E7=95=99=E7=A9=BA=E5=91=8A=E8=AD=A6=E4=B8=8E=E8=B4=9F?= =?UTF-8?q?=E6=8E=A7=E6=B5=8B=E8=AF=95=20(#15)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Agent-Executor: agy Agent-Model: gemini-3.8-flash-high Agent-Effort: unknown Dispatch-Id: dlg-20260923-082221-6ccc70 Task-Id: MediaResolverAPI-20260923-02 --- tests/test_public_base_url_warning.py | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/test_public_base_url_warning.py diff --git a/tests/test_public_base_url_warning.py b/tests/test_public_base_url_warning.py new file mode 100644 index 0000000..41f4b2e --- /dev/null +++ b/tests/test_public_base_url_warning.py @@ -0,0 +1,39 @@ +import pytest +from fastapi.testclient import TestClient + +from app.core.config import settings +from app.main import app + + +def _get_log_lines(tmp_path, pattern): + log_files = list(tmp_path.glob("app_*.log")) + assert len(log_files) == 1, f"Expected 1 app log file, found {len(log_files)}" + content = log_files[0].read_text(encoding="utf-8") + return [line for line in content.splitlines() if pattern in line] + + +@pytest.mark.parametrize("empty_val", ["", " "]) +def test_public_base_url_warning_when_empty(monkeypatch, tmp_path, empty_val): + monkeypatch.setattr(settings, "LOG_FILE_PATH", str(tmp_path)) + monkeypatch.setattr(settings, "PUBLIC_BASE_URL", empty_val) + + with TestClient(app, raise_server_exceptions=False) as client: + resp = client.get("/") + assert resp.status_code == 200 + assert resp.json().get("status") == "running" + + matches = _get_log_lines(tmp_path, "PUBLIC_BASE_URL 未设置") + assert len(matches) == 1 + assert "data.video_url 将按请求 Host 推导,跨机调用会拿到不可用地址" in matches[0] + + +def test_public_base_url_no_warning_when_set(monkeypatch, tmp_path): + monkeypatch.setattr(settings, "LOG_FILE_PATH", str(tmp_path)) + monkeypatch.setattr(settings, "PUBLIC_BASE_URL", "https://api.example.com") + + with TestClient(app, raise_server_exceptions=False) as client: + resp = client.get("/") + assert resp.status_code == 200 + + matches = _get_log_lines(tmp_path, "PUBLIC_BASE_URL 未设置") + assert len(matches) == 0