From c46d05ef294c91dc2369b367d51cb139742e025f Mon Sep 17 00:00:00 2001 From: Bruce Schultz Date: Thu, 29 Feb 2024 14:02:47 +0100 Subject: [PATCH] chore(server): add healthcheck ep and to Dockerfile --- Dockerfile | 2 ++ gateway/models.py | 6 ++++++ gateway/server.py | 35 +++++++++++++++++++++++------------ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2ae62e5..7772c27 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,3 +24,5 @@ COPY ./gateway/ ./gateway/ EXPOSE 5000 ENTRYPOINT ["python", "-m", "gateway.cli", "serve"] + +HEALTHCHECK CMD curl --fail http://localhost:5000/health || exit 1 diff --git a/gateway/models.py b/gateway/models.py index 28790da..de92159 100644 --- a/gateway/models.py +++ b/gateway/models.py @@ -6,6 +6,12 @@ # Method models +class HealthCheck(BaseModel): + """Response model to validate and return when performing a health check.""" + + status: str = "OK" + + class User(BaseModel): """Example User output""" diff --git a/gateway/server.py b/gateway/server.py index be1c716..4490988 100644 --- a/gateway/server.py +++ b/gateway/server.py @@ -1,8 +1,10 @@ """Methods for verifying auth.""" import uvicorn -from fastapi import FastAPI, Security +from fastapi import FastAPI +from starlette import status -from gateway.auth import idp_settings, oauth2_scheme +from gateway.auth import idp_settings +from gateway.models import HealthCheck from gateway.routers.k8s import k8s_router from gateway.routers.results import results_router @@ -36,16 +38,25 @@ # ) -@app.get("/unsecure") -async def unsecure_test() -> dict: - """Default greeting.""" - return {"message": "Howdy anonymous"} - - -@app.get("/secure") -async def secure_test(token: str = Security(oauth2_scheme)): - """Secured response greeting.""" - return token +@app.get( + "/health", + tags=["healthcheck"], + summary="Perform a Health Check", + response_description="Return HTTP Status Code 200 (OK)", + status_code=status.HTTP_200_OK, + response_model=HealthCheck, +) +def get_health() -> HealthCheck: + """ + ## Perform a Health Check + Endpoint to perform a healthcheck on. This endpoint can primarily be used Docker + to ensure a robust container orchestration and management is in place. Other + services which rely on proper functioning of the API service will not deploy if this + endpoint returns any other HTTP status code except 200 (OK). + Returns: + HealthCheck: Returns a JSON response with the health status + """ + return HealthCheck(status="OK") app.include_router(