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

✨Add health endpoint #28

Merged
merged 1 commit into from
Mar 26, 2024
Merged
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
23 changes: 21 additions & 2 deletions duty_board/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
from pytz.exceptions import UnknownTimeZoneError
from pytz.tzinfo import BaseTzInfo
from sqladmin import Admin
from sqlalchemy import select
from sqlalchemy import select, text
from sqlalchemy.orm.session import Session as SASession
from starlette import status
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import FileResponse, HTMLResponse, Response
from starlette.responses import FileResponse, HTMLResponse, JSONResponse, Response
from starlette.staticfiles import StaticFiles
from tzlocal import get_localzone

Expand Down Expand Up @@ -145,6 +146,24 @@ def favicon_ico() -> FileResponse:
raise HTTPException(status_code=500, detail="Company logo could not be found.")


@app.get(
"/health",
tags=["healthcheck"],
summary="Perform a Health Check",
status_code=status.HTTP_200_OK,
response_model=None,
)
def get_health() -> JSONResponse:
try:
with create_session() as session:
session.execute(text("SELECT 1"))
return JSONResponse(content={"result": "OK", "error": None}, status_code=status.HTTP_200_OK)
except Exception as exc:
return JSONResponse(
content={"result": "ERROR", "error": str(exc)}, status_code=status.HTTP_503_SERVICE_UNAVAILABLE
)


@app.get("/{full_path:path}", response_class=HTMLResponse, include_in_schema=False)
async def accept_all() -> FileResponse:
return FileResponse(CURRENT_DIR / "www" / "dist" / "index.html")
Loading