ver0.1
This commit is contained in:
42
main.py
42
main.py
@@ -1,5 +1,7 @@
|
||||
import logging
|
||||
import sys
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
@@ -12,6 +14,22 @@ from app.database import check_database, engine
|
||||
from app.dependencies import build_container
|
||||
|
||||
|
||||
def _configure_uvicorn_access_log_filters() -> None:
|
||||
"""第三方或 Demo 若轮询 pending-confirmation,无条目时 404 为常态;压低 uvicorn access 刷屏。"""
|
||||
|
||||
class _SuppressPendingPoll404(logging.Filter):
|
||||
def filter(self, record: logging.LogRecord) -> bool:
|
||||
try:
|
||||
msg = record.getMessage()
|
||||
except Exception:
|
||||
return True
|
||||
if "/pending-confirmation" in msg and "GET" in msg and " 404 " in msg:
|
||||
return False
|
||||
return True
|
||||
|
||||
logging.getLogger("uvicorn.access").addFilter(_SuppressPendingPoll404())
|
||||
|
||||
|
||||
def configure_logging() -> None:
|
||||
"""集中配置 loguru sink;由 create_app 显式调用,避免 import-time 副作用。"""
|
||||
logger.remove()
|
||||
@@ -45,6 +63,7 @@ async def lifespan(app: FastAPI):
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
configure_logging()
|
||||
_configure_uvicorn_access_log_filters()
|
||||
application = FastAPI(
|
||||
title="Operation Room Monitor",
|
||||
lifespan=lifespan,
|
||||
@@ -81,12 +100,23 @@ app = create_app()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
uvicorn.run(
|
||||
"main:app",
|
||||
host=settings.server_host,
|
||||
port=settings.server_port,
|
||||
reload=False,
|
||||
)
|
||||
root = Path(__file__).resolve().parent
|
||||
kwargs: dict = {
|
||||
"host": settings.server_host,
|
||||
"port": settings.server_port,
|
||||
}
|
||||
if settings.server_reload:
|
||||
kwargs["reload"] = True
|
||||
kwargs["reload_dirs"] = [str(root)]
|
||||
kwargs["reload_includes"] = ["*.py"]
|
||||
kwargs["reload_excludes"] = [
|
||||
"**/.venv/**",
|
||||
"**/__pycache__/**",
|
||||
"**/web/**",
|
||||
"**/scripts/demo_client/**",
|
||||
"**/.git/**",
|
||||
]
|
||||
uvicorn.run("main:app", **kwargs)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user