-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
63 lines (48 loc) · 1.81 KB
/
main.py
File metadata and controls
63 lines (48 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
FastAPI Admin Dashboard - Application Entry Point
A production-ready admin dashboard API with JWT authentication,
role-based access control, and async database operations.
"""
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.config import settings
from app.database import engine, Base
from app.api.auth import router as auth_router
from app.api.dashboard import router as dashboard_router
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan manager for startup and shutdown events."""
# Startup: create database tables
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield
# Shutdown: dispose engine
await engine.dispose()
def create_application() -> FastAPI:
"""Factory function to create and configure the FastAPI application."""
app = FastAPI(
title=settings.PROJECT_NAME,
description="Admin Dashboard API with JWT Auth and RBAC",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc",
lifespan=lifespan,
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=settings.ALLOWED_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Register routers
app.include_router(auth_router, prefix="/api/v1/auth", tags=["Authentication"])
app.include_router(dashboard_router, prefix="/api/v1/dashboard", tags=["Dashboard"])
return app
app = create_application()
@app.get("/health", tags=["Health"])
async def health_check():
"""Health check endpoint for container orchestration."""
return {"status": "healthy", "version": "1.0.0"}