feat: add healthcheck
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import logging
|
||||
import requests
|
||||
from typing import Annotated
|
||||
from fastapi import FastAPI, Header, HTTPException
|
||||
from fastapi import FastAPI, Header, HTTPException, status
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import FileResponse
|
||||
from pydantic import BaseModel
|
||||
@@ -18,6 +19,20 @@ app.add_middleware(
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
block_endpoints = ["/health"]
|
||||
|
||||
|
||||
class LogFilter(logging.Filter):
|
||||
def filter(self, record):
|
||||
if record.args and len(record.args) >= 3:
|
||||
if record.args[2] in block_endpoints:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
uvicorn_logger = logging.getLogger("uvicorn.access")
|
||||
uvicorn_logger.addFilter(LogFilter())
|
||||
|
||||
|
||||
class AuthHeaders(BaseModel):
|
||||
Authorization: str
|
||||
@@ -31,11 +46,26 @@ class AuthHeaders(BaseModel):
|
||||
return False
|
||||
|
||||
|
||||
class HealthCheck(BaseModel):
|
||||
"""Response model to validate and return when performing a health check."""
|
||||
|
||||
status: str = "OK"
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def home():
|
||||
return FileResponse("index.html")
|
||||
|
||||
|
||||
@app.get(
|
||||
"/health",
|
||||
response_model=HealthCheck,
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
def get_health() -> HealthCheck:
|
||||
return HealthCheck(status="OK")
|
||||
|
||||
|
||||
@app.get("/favicon.ico")
|
||||
async def favico():
|
||||
return FileResponse("favicon.ico")
|
||||
|
||||
Reference in New Issue
Block a user