Automated Action acf2757384 Fix Supervisor startup errors and improve application resilience
- Update database path configuration for better compatibility
- Add comprehensive logging system with error handling
- Create supervisord.conf with proper configuration
- Add environment variable example file
- Enhance health check endpoint to report database status
- Add detailed startup and shutdown event handlers
- Update documentation with troubleshooting information
- Update requirements with additional helpful packages
2025-06-02 22:42:07 +00:00

90 lines
2.6 KiB
Python

import uvicorn
import logging
from logging.config import dictConfig
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from app.api.v1.api import api_router
from app.core.config import settings
from app.core.logging import LogConfig
# Setup logging
dictConfig(LogConfig().dict())
logger = logging.getLogger("blogging_api")
app = FastAPI(
title=settings.PROJECT_NAME,
description="Blogging API with FastAPI and SQLite",
version="0.1.0",
docs_url="/docs",
redoc_url="/redoc",
openapi_url="/openapi.json",
)
# Exception handlers
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
logger.error(f"Unhandled exception: {exc}", exc_info=True)
return JSONResponse(
status_code=500,
content={"detail": "Internal server error. Please check the logs for more details."},
)
# 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)
# Application lifecycle events
@app.on_event("startup")
async def startup_event():
logger.info("Application starting up...")
# Make sure we can initialize database connection
try:
logger.info("Database engine initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize database: {e}", exc_info=True)
# We allow the app to start even with DB errors, to avoid restart loops
@app.on_event("shutdown")
async def shutdown_event():
logger.info("Application shutting down...")
# Health check endpoint with database status
@app.get("/health", tags=["health"])
async def health_check():
from app.db.session import SessionLocal
health_status = {"status": "ok", "database": "unknown"}
if SessionLocal:
try:
db = SessionLocal()
db.execute("SELECT 1")
db.close()
health_status["database"] = "ok"
except Exception as e:
logger.error(f"Database health check failed: {e}")
health_status["database"] = "error"
health_status["status"] = "degraded"
else:
health_status["database"] = "error"
health_status["status"] = "degraded"
return health_status
if __name__ == "__main__":
import os
port = int(os.environ.get("PORT", 8000))
uvicorn.run(
"main:app",
host="0.0.0.0",
port=port,
reload=True,
)