This repository has been archived on 2026-01-19. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
DevDen/backend/app/main.py
2026-01-15 22:07:32 +01:00

88 lines
2.2 KiB
Python

import logging
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from .api import auth, chat
from .config import settings
from .services.provider_manager import provider_manager
# Setup logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('/app/devden.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
app = FastAPI(
title="DevDen API", description="AI chat backend for DevDen", version="1.0.0"
)
# CORS Configuration
app.add_middleware(
CORSMiddleware,
allow_origins=[settings.FRONTEND_URL, "http://localhost:3000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include routers
app.include_router(auth.router)
app.include_router(chat.router)
@app.exception_handler(Exception)
async def global_exception_handler(request, exc):
"""Global exception handler to log all errors"""
logger.error(f"Unhandled exception: {exc}", {
"url": str(request.url),
"method": request.method,
"headers": dict(request.headers),
"traceback": str(exc)
})
return JSONResponse(
status_code=500,
content={"detail": "Internal server error"}
)
@app.get("/health")
async def health_check():
"""Health check endpoint"""
logger.info("Health check requested")
return JSONResponse(
content={
"status": "healthy",
"providers": provider_manager.get_available_providers(),
}
)
@app.get("/logs")
async def get_logs():
"""Get recent log entries (for debugging)"""
try:
with open('/app/devden.log', 'r') as f:
lines = f.readlines()[-50:] # Last 50 lines
return {"logs": lines}
except Exception as e:
logger.error(f"Failed to read logs: {e}")
return {"error": "Failed to read logs"}
@app.on_event("startup")
async def startup_event():
logger.info("DevDen API starting up...")
logger.info(f"Available providers: {provider_manager.get_available_providers()}")
@app.on_event("shutdown")
async def shutdown_event():
logger.info("DevDen API shutting down...")