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() 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