Allow all origins and add DB check to health endpoint
This commit is contained in:
@ -121,8 +121,7 @@ origins = [
|
|||||||
|
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=origins,
|
allow_origins=["*"], # Abrir temporalmente para diagnóstico
|
||||||
allow_origin_regex="https://.*sibu.*\.vercel\.app", # Permitir subdominios de vercel con 'sibu'
|
|
||||||
allow_credentials=True,
|
allow_credentials=True,
|
||||||
allow_methods=["*"],
|
allow_methods=["*"],
|
||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
@ -134,8 +133,9 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
@app.exception_handler(Exception)
|
@app.exception_handler(Exception)
|
||||||
async def global_exception_handler(request, exc):
|
async def global_exception_handler(request, exc):
|
||||||
logger.error(f"Global error: {exc}", exc_info=True)
|
# Loguear de forma segura sin caracteres especiales que rompan el terminal
|
||||||
return {"detail": "Internal Server Error", "message": str(exc)}
|
logger.error(f"Global error on {request.url.path}: {str(exc)}")
|
||||||
|
return {"detail": "Internal Server Error", "message": "Verify database connection or payload"}
|
||||||
|
|
||||||
# Ensure upload directories exist
|
# Ensure upload directories exist
|
||||||
for sub in ["profiles", "vehicles", "businesses"]:
|
for sub in ["profiles", "vehicles", "businesses"]:
|
||||||
@ -167,6 +167,17 @@ async def root():
|
|||||||
|
|
||||||
|
|
||||||
@app.get("/health")
|
@app.get("/health")
|
||||||
async def health():
|
async def health(session: Session = Depends(get_session)):
|
||||||
"""Health check endpoint."""
|
"""Health check endpoint connecting to DB."""
|
||||||
return {"status": "healthy", "environment": settings.environment}
|
db_status = "connected"
|
||||||
|
try:
|
||||||
|
from sqlalchemy import text
|
||||||
|
session.execute(text("SELECT 1"))
|
||||||
|
except Exception as e:
|
||||||
|
db_status = f"disconnected: {str(e)}"
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status": "healthy" if db_status == "connected" else "degraded",
|
||||||
|
"environment": settings.environment,
|
||||||
|
"database": db_status
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user