add /health endpoint

This commit is contained in:
johnpccd 2025-05-24 15:07:46 +02:00
parent 365569aa52
commit 88905a2684

23
main.py
View File

@ -4,6 +4,7 @@ from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from fastapi.openapi.docs import get_swagger_ui_html from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.openapi.utils import get_openapi from fastapi.openapi.utils import get_openapi
import time
# Import API routers # Import API routers
from src.api.v1 import teams, users, images, auth, search from src.api.v1 import teams, users, images, auth, search
@ -29,6 +30,23 @@ app = FastAPI(
openapi_url="/api/v1/openapi.json" openapi_url="/api/v1/openapi.json"
) )
# Add request logging middleware
@app.middleware("http")
async def log_requests(request: Request, call_next):
start_time = time.time()
# Log incoming request details
logger.info(f"Incoming request: {request.method} {request.url}")
logger.info(f"Headers: {dict(request.headers)}")
logger.info(f"Client: {request.client}")
response = await call_next(request)
process_time = time.time() - start_time
logger.info(f"Request completed in {process_time:.4f}s with status {response.status_code}")
return response
# Connect to database # Connect to database
try: try:
db.connect_to_database() db.connect_to_database()
@ -130,6 +148,11 @@ def custom_openapi():
app.openapi = custom_openapi app.openapi = custom_openapi
@app.get("/health", include_in_schema=False)
async def health_check():
"""Health check endpoint for monitoring and client connectivity tests"""
return {"status": "healthy", "message": "API is running"}
@app.get("/", include_in_schema=False) @app.get("/", include_in_schema=False)
async def root(): async def root():
return {"message": "Welcome to the Image Management API. Please see /docs for API documentation."} return {"message": "Welcome to the Image Management API. Please see /docs for API documentation."}