From 72a1f47c5c0a6fa2d09c47bb6da94f1c7dfdad58 Mon Sep 17 00:00:00 2001 From: Moritz Kirmse Date: Wed, 22 Jan 2025 14:25:45 +0100 Subject: [PATCH 1/2] enforce strict health checking --- backend/maelstro/__init__.py | 6 +++--- backend/maelstro/main.py | 15 +++++++++------ backend/maelstro/scripts/code_check.sh | 2 +- backend/maelstro/scripts/health_check.py | 2 +- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/backend/maelstro/__init__.py b/backend/maelstro/__init__.py index 39938b1..29a1cda 100644 --- a/backend/maelstro/__init__.py +++ b/backend/maelstro/__init__.py @@ -6,7 +6,7 @@ from fastapi_cli.utils.cli import get_uvicorn_log_config -def dev(): +def dev() -> None: """ Dev server entrypoint: special configuration: @@ -25,7 +25,7 @@ def dev(): ) -def docker_dev(): +def docker_dev() -> None: """ Dev server entrypoint for running the server inside a docker container: special configuration: @@ -43,7 +43,7 @@ def docker_dev(): ) -def prod(): +def prod() -> None: """ Server entrypoint for running the server inside a docker container: """ diff --git a/backend/maelstro/main.py b/backend/maelstro/main.py index 468c35f..c2ea055 100644 --- a/backend/maelstro/main.py +++ b/backend/maelstro/main.py @@ -2,7 +2,7 @@ Main backend app setup """ -from typing import Annotated +from typing import Annotated, Any from fastapi import FastAPI, Request, Response, Header @@ -13,7 +13,7 @@ @app.head("/") @app.get("/") -def root_page(): +def root_page() -> dict[str, str]: """ Hello world dummy route """ @@ -30,7 +30,7 @@ def user_page( ] = None, sec_proxy: Annotated[str | None, Header(include_in_schema=False)] = None, sec_orgname: Annotated[str | None, Header(include_in_schema=False)] = None, -): +) -> dict[str, str | None]: """ Display user information provided by gateway """ @@ -53,7 +53,7 @@ def user_page( @app.delete("/debug") @app.options("/debug") @app.patch("/debug") -def debug_page(request: Request): +def debug_page(request: Request) -> dict[str, Any]: """ Display details of query including headers. This may be useful in development to check all the headers provided by the gateway. @@ -80,7 +80,10 @@ def debug_page(request: Request): @app.get("/health") -def health_check(response: Response): +def health_check( + response: Response, + sec_username: Annotated[str | None, Header(include_in_schema=False)] = None, +) -> dict[str, str | None]: """ Health check to make sure the server is up and running For test purposes, the server is reported healthy only from the 5th request onwards @@ -90,4 +93,4 @@ def health_check(response: Response): app.state.health_countdown -= 1 response.status_code = 404 status = "unhealthy" - return {"status": status, "user": None} + return {"status": status, "user": sec_username} diff --git a/backend/maelstro/scripts/code_check.sh b/backend/maelstro/scripts/code_check.sh index 5bc7ca8..46800bd 100755 --- a/backend/maelstro/scripts/code_check.sh +++ b/backend/maelstro/scripts/code_check.sh @@ -3,7 +3,7 @@ cd "$(dirname "$0")"/.. poetry run black --check . && \ -poetry run mypy . && \ +poetry run mypy --strict . && \ poetry run pyflakes . && \ poetry run pylint . diff --git a/backend/maelstro/scripts/health_check.py b/backend/maelstro/scripts/health_check.py index 8c1e30c..ff58c8b 100755 --- a/backend/maelstro/scripts/health_check.py +++ b/backend/maelstro/scripts/health_check.py @@ -3,5 +3,5 @@ import requests -def check(): +def check() -> None: assert requests.get("http://localhost:8000/health").status_code == 200 From cf237975d05c2bd6fdaa175eebb1bfee15902588 Mon Sep 17 00:00:00 2001 From: Moritz Kirmse Date: Wed, 22 Jan 2025 14:37:35 +0100 Subject: [PATCH 2/2] remove remaining "poetry run" --- backend/maelstro/scripts/code_check.sh | 8 ++++---- backend/maelstro/scripts/code_fix.sh | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/backend/maelstro/scripts/code_check.sh b/backend/maelstro/scripts/code_check.sh index 46800bd..3410252 100755 --- a/backend/maelstro/scripts/code_check.sh +++ b/backend/maelstro/scripts/code_check.sh @@ -2,9 +2,9 @@ cd "$(dirname "$0")"/.. -poetry run black --check . && \ -poetry run mypy --strict . && \ -poetry run pyflakes . && \ -poetry run pylint . +black --check . && \ +mypy --strict . && \ +pyflakes . && \ +pylint . ! (( $? & 7 )) # mask exit code for minor findings (refactor, convention, usage) diff --git a/backend/maelstro/scripts/code_fix.sh b/backend/maelstro/scripts/code_fix.sh index c223dc4..279b60b 100755 --- a/backend/maelstro/scripts/code_fix.sh +++ b/backend/maelstro/scripts/code_fix.sh @@ -2,4 +2,4 @@ cd "$(dirname "$0")"/.. -poetry run black . +black .