From 21e30b2e169d6570b7247690d22f935c770afa21 Mon Sep 17 00:00:00 2001
From: Guillem Borrell
Date: Sat, 18 May 2024 00:02:14 +0200
Subject: [PATCH] Healthcheck
---
README.md | 10 +++++++++-
src/hellocomputer/main.py | 31 ++++++++++++++++++++++++++++-
src/hellocomputer/static/index.html | 7 +++++++
3 files changed, 46 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index d8e6f28..4f204c9 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,11 @@
# hellocomputer
-Analytics with natural language
\ No newline at end of file
+Analytics with natural language
+
+```
+uv pip install -r requirements.in
+```
+
+```
+uvicorn hellocomputer.main:app --host localhost
+```
\ No newline at end of file
diff --git a/src/hellocomputer/main.py b/src/hellocomputer/main.py
index cb42d15..9ff9e09 100644
--- a/src/hellocomputer/main.py
+++ b/src/hellocomputer/main.py
@@ -1,12 +1,41 @@
-from fastapi import FastAPI
+from fastapi import FastAPI, status
from fastapi.staticfiles import StaticFiles
import hellocomputer
from pathlib import Path
+from pydantic import BaseModel
static_path = Path(hellocomputer.__file__).parent / "static"
app = FastAPI()
+
+class HealthCheck(BaseModel):
+ """Response model to validate and return when performing a health check."""
+
+ status: str = "OK"
+
+
+@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.mount(
"/",
StaticFiles(directory=static_path, html=True, packages=["bootstrap4"]),
diff --git a/src/hellocomputer/static/index.html b/src/hellocomputer/static/index.html
index 4b17aa0..012e1dd 100644
--- a/src/hellocomputer/static/index.html
+++ b/src/hellocomputer/static/index.html
@@ -7,10 +7,17 @@
Bootstrap demo
+
+