
- Move wildcard import from app.db.all_models to module level - Fix SyntaxError that was preventing application startup - Ensure all models are registered with SQLAlchemy Base
90 lines
2.0 KiB
Python
90 lines
2.0 KiB
Python
import logging
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.openapi.utils import get_openapi
|
|
|
|
from app.api.api_v1.api import api_router
|
|
from app.core.config import settings
|
|
|
|
# Import all models to ensure they are registered with Base
|
|
from app.db.all_models import * # noqa
|
|
from app.db.base import Base
|
|
from app.db.session import engine
|
|
|
|
# Set up logging
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Create database tables
|
|
def create_tables():
|
|
try:
|
|
logger.info("Creating database tables")
|
|
Base.metadata.create_all(bind=engine)
|
|
logger.info("Database tables created successfully")
|
|
except Exception as e:
|
|
logger.error(f"Error creating database tables: {e}")
|
|
raise
|
|
|
|
# Initialize FastAPI app
|
|
app = FastAPI(
|
|
title=settings.PROJECT_NAME,
|
|
openapi_url="/openapi.json",
|
|
docs_url="/docs",
|
|
redoc_url="/redoc",
|
|
)
|
|
|
|
# Set all CORS enabled origins
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Include API router
|
|
app.include_router(api_router, prefix=settings.API_V1_STR)
|
|
|
|
# Create tables on startup if they don't exist
|
|
@app.on_event("startup")
|
|
async def startup_event():
|
|
create_tables()
|
|
|
|
|
|
@app.get("/", tags=["status"])
|
|
async def root():
|
|
"""
|
|
Root endpoint that returns basic service information.
|
|
"""
|
|
return {
|
|
"name": settings.PROJECT_NAME,
|
|
"docs": "/docs",
|
|
"health": "/health"
|
|
}
|
|
|
|
|
|
@app.get("/health", tags=["status"])
|
|
async def health():
|
|
"""
|
|
Health check endpoint.
|
|
"""
|
|
return {"status": "healthy"}
|
|
|
|
|
|
def custom_openapi():
|
|
if app.openapi_schema:
|
|
return app.openapi_schema
|
|
|
|
openapi_schema = get_openapi(
|
|
title=settings.PROJECT_NAME,
|
|
version=settings.VERSION,
|
|
description="A FastAPI Todo API",
|
|
routes=app.routes,
|
|
)
|
|
|
|
app.openapi_schema = openapi_schema
|
|
return app.openapi_schema
|
|
|
|
|
|
app.openapi = custom_openapi |