59 lines
1.2 KiB
Python
59 lines
1.2 KiB
Python
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from contextlib import asynccontextmanager
|
|
from .core.config import settings
|
|
from .api.v1 import api_router
|
|
from .core.database import get_db
|
|
from .core.init_db import init_default_data
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""Handle startup and shutdown events"""
|
|
# Startup
|
|
db: Session = next(get_db())
|
|
try:
|
|
init_default_data(db)
|
|
finally:
|
|
db.close()
|
|
|
|
yield
|
|
|
|
# Shutdown (if needed)
|
|
pass
|
|
|
|
|
|
app = FastAPI(
|
|
title=settings.APP_NAME,
|
|
version=settings.APP_VERSION,
|
|
openapi_url=f"{settings.API_V1_PREFIX}/openapi.json",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# Set up CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.BACKEND_CORS_ORIGINS,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Include API router
|
|
app.include_router(api_router, prefix=settings.API_V1_PREFIX)
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {
|
|
"message": f"Welcome to {settings.APP_NAME}",
|
|
"version": settings.APP_VERSION,
|
|
"docs": "/docs"
|
|
}
|
|
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
return {"status": "healthy"}
|