Initial commit: FastAPI app with Loguru, PostgreSQL, and Docker Compose

Made-with: Cursor
This commit is contained in:
Kevin
2026-04-20 17:58:03 +08:00
commit d1a3d029ec
15 changed files with 834 additions and 0 deletions

58
main.py Normal file
View File

@@ -0,0 +1,58 @@
import sys
from contextlib import asynccontextmanager
import uvicorn
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from loguru import logger
from sqlalchemy.exc import SQLAlchemyError
from app.database import check_database, engine
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()
logger.info("Database connection verified")
yield
await engine.dispose()
logger.info("Database engine disposed")
app = FastAPI(
title="Operation Room Monitor",
lifespan=lifespan,
)
@app.get("/health")
async def health():
logger.debug("Health check")
try:
await check_database()
except SQLAlchemyError as exc:
logger.warning("Health check: database unavailable: {}", exc)
return JSONResponse(
status_code=503,
content={"status": "degraded", "database": "unavailable"},
)
return {"status": "ok", "database": "connected"}
def main() -> None:
uvicorn.run(
"main:app",
host="0.0.0.0",
port=8000,
reload=True,
)
if __name__ == "__main__":
main()