Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
39 changes: 39 additions & 0 deletions tests/test_public_base_url_warning.py
Original file line number Diff line number Diff line change
@@ -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
Loading