Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enforce strict health checking #8

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions backend/maelstro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from fastapi_cli.utils.cli import get_uvicorn_log_config


def dev():
def dev() -> None:
"""
Dev server entrypoint:
special configuration:
Expand All @@ -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:
Expand All @@ -43,7 +43,7 @@ def docker_dev():
)


def prod():
def prod() -> None:
"""
Server entrypoint for running the server inside a docker container:
"""
Expand Down
15 changes: 9 additions & 6 deletions backend/maelstro/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Main backend app setup
"""

from typing import Annotated
from typing import Annotated, Any
from fastapi import FastAPI, Request, Response, Header


Expand All @@ -13,7 +13,7 @@

@app.head("/")
@app.get("/")
def root_page():
def root_page() -> dict[str, str]:
"""
Hello world dummy route
"""
Expand All @@ -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
"""
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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}
8 changes: 4 additions & 4 deletions backend/maelstro/scripts/code_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

cd "$(dirname "$0")"/..

poetry run black --check . && \
poetry run mypy . && \
poetry run pyflakes . && \
poetry run pylint .
black --check . && \
mypy --strict . && \
pyflakes . && \
pylint .

! (( $? & 7 )) # mask exit code for minor findings (refactor, convention, usage)
2 changes: 1 addition & 1 deletion backend/maelstro/scripts/code_fix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

cd "$(dirname "$0")"/..

poetry run black .
black .
2 changes: 1 addition & 1 deletion backend/maelstro/scripts/health_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import requests


def check():
def check() -> None:
assert requests.get("http://localhost:8000/health").status_code == 200