- Load consumable catalog XLSX with openpyxl and drop the pandas dependency. - Add optional demo CORS settings and FastAPI CORSMiddleware for browser clients. - Add scripts/demo_client static page and local server for API smoke tests. Made-with: Cursor
67 lines
1.7 KiB
Python
67 lines
1.7 KiB
Python
import sys
|
|
from contextlib import asynccontextmanager
|
|
|
|
import uvicorn
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from loguru import logger
|
|
|
|
from app.api import router as api_router
|
|
from app.config import settings
|
|
from app.database import check_database, engine, init_db_schema
|
|
from app.dependencies import camera_session_manager
|
|
|
|
logger.remove()
|
|
logger.add(
|
|
sys.stderr,
|
|
format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan> - <level>{message}</level>",
|
|
)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
await check_database()
|
|
await init_db_schema()
|
|
logger.info("Database connection verified and schema ensured")
|
|
await camera_session_manager.start_archive_retry_loop()
|
|
yield
|
|
await camera_session_manager.shutdown()
|
|
await engine.dispose()
|
|
logger.info("Database engine disposed")
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
application = FastAPI(
|
|
title="Operation Room Monitor",
|
|
lifespan=lifespan,
|
|
)
|
|
if settings.demo_cors_enabled:
|
|
origins = settings.parsed_demo_cors_origins()
|
|
if origins:
|
|
application.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=origins,
|
|
allow_credentials=origins != ["*"],
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
logger.info("CORS enabled for demo client; origins={}", origins)
|
|
application.include_router(api_router)
|
|
return application
|
|
|
|
|
|
app = create_app()
|
|
|
|
|
|
def main() -> None:
|
|
uvicorn.run(
|
|
"main:app",
|
|
host="0.0.0.0",
|
|
port=38080,
|
|
reload=True,
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|