"""
RTSport FastAPI Application Factory
"""
from fastapi import FastAPI, Depends, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
from app.config import settings
from app.database import init_db
from app.api import roster, cases, events, auth, dashboard, body_parts, athletes, admin
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan events"""
# Startup
print(f"Starting {settings.project_name}...")
# init_db() # Uncomment to auto-create tables on startup
yield
# Shutdown
print(f"Shutting down {settings.project_name}...")
def create_application() -> FastAPI:
"""Application factory pattern"""
app = FastAPI(
title=settings.project_name,
description="Athletic training case management API for scholastic sports",
version="0.2.0",
docs_url="/docs" if settings.debug else None,
redoc_url="/redoc" if settings.debug else None,
lifespan=lifespan,
)
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include routers - paths match OpenAPI v0.3 spec
app.include_router(
auth.router,
prefix=f"{settings.api_v1_prefix}/auth",
)
app.include_router(
roster.router,
prefix=f"{settings.api_v1_prefix}/roster",
)
app.include_router(
cases.router,
prefix=f"{settings.api_v1_prefix}/cases",
)
app.include_router(
events.router,
prefix=f"{settings.api_v1_prefix}/events",
)
app.include_router(
dashboard.router,
prefix=f"{settings.api_v1_prefix}/dashboard",
)
app.include_router(
body_parts.router,
prefix=f"{settings.api_v1_prefix}/body-parts",
)
app.include_router(
athletes.router,
prefix=f"{settings.api_v1_prefix}/athletes",
)
app.include_router(
admin.router,
prefix=f"{settings.api_v1_prefix}/admin",
)
# Health check endpoint
@app.get("/health")
async def health_check():
return {"status": "healthy", "service": settings.project_name}
# Root endpoint
@app.get("/")
async def root():
return {
"name": settings.project_name,
"version": "0.3.0",
"docs": "/docs" if settings.debug else None,
}
return app
Create the application instance
app = create_application()
if name == "main":
import uvicorn
uvicorn.run(
"app.main:app",
host="0.0.0.0",
port=8000,
reload=settings.debug,
)