Files
poona-ams/backend/app/main.py
T

65 lines
1.9 KiB
Python

import logging
import traceback
from contextlib import asynccontextmanager
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from .core.db import SessionLocal, engine
from .core.exceptions import CodedHTTPException, coded_http_exception_handler
from .models import Base
from .routers import assets, attachments, auth, health, masters, settings, users
from .services.settings_service import ensure_default_settings
logging.basicConfig(
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
level=logging.INFO,
)
logger = logging.getLogger("poona_ams")
@asynccontextmanager
async def lifespan(app: FastAPI):
Base.metadata.create_all(bind=engine)
with SessionLocal() as db:
ensure_default_settings(db)
db.commit()
logger.info("Database tables created/verified.")
logger.info("Poona Hospital AMS started.")
yield
app = FastAPI(
title="Poona Hospital AMS",
version="0.1.0",
docs_url="/docs",
lifespan=lifespan,
)
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173", "http://127.0.0.1:5173"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.add_exception_handler(CodedHTTPException, coded_http_exception_handler)
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
logger.error(f"Unhandled exception: {traceback.format_exc()}")
return JSONResponse(
status_code=500,
content={"detail": "An internal error occurred.", "code": "INTERNAL_ERROR"},
)
app.include_router(health.router, prefix="/api")
app.include_router(auth.router, prefix="/api")
app.include_router(settings.router, prefix="/api")
app.include_router(masters.router, prefix="/api")
app.include_router(users.router, prefix="/api")
app.include_router(assets.router, prefix="/api")
app.include_router(attachments.router, prefix="/api")