56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
import uvicorn
|
|
from fastapi import Depends, FastAPI
|
|
from sqlalchemy import text
|
|
from sqlalchemy.orm import Session
|
|
from starlette.middleware.cors import CORSMiddleware
|
|
|
|
from app.api.api import api_router
|
|
from app.core.config import settings
|
|
from app.db.session import get_db
|
|
|
|
app = FastAPI(
|
|
title=settings.PROJECT_NAME,
|
|
openapi_url=f"{settings.API_V1_STR}/openapi.json",
|
|
description="Logistics Management System API",
|
|
version="0.1.0",
|
|
)
|
|
|
|
# Set all CORS enabled origins
|
|
if settings.BACKEND_CORS_ORIGINS:
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
app.include_router(api_router, prefix=settings.API_V1_STR)
|
|
|
|
|
|
@app.get("/health", tags=["Health"])
|
|
async def health_check(db: Session = Depends(get_db)):
|
|
"""
|
|
Root health check endpoint to verify the application is running correctly
|
|
and database connection is working.
|
|
"""
|
|
health_data = {
|
|
"status": "ok",
|
|
"api": "healthy",
|
|
"database": "healthy"
|
|
}
|
|
|
|
# Check database connectivity
|
|
try:
|
|
# Execute a simple query
|
|
db.execute(text("SELECT 1"))
|
|
except Exception as e:
|
|
health_data["database"] = "unhealthy"
|
|
health_data["status"] = "error"
|
|
health_data["database_error"] = str(e)
|
|
|
|
return health_data
|
|
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) |