|
3 | 3 | import sys
|
4 | 4 | from contextlib import asynccontextmanager
|
5 | 5 | from loguru import logger as log
|
6 |
| -from fastapi import FastAPI, Request |
| 6 | +from fastapi import FastAPI, Request, Depends |
7 | 7 | from fastapi.middleware.cors import CORSMiddleware
|
8 | 8 | from fastapi.templating import Jinja2Templates
|
9 |
| -from fastapi.responses import RedirectResponse, JSONResponse |
| 9 | +from fastapi.responses import RedirectResponse, JSONResponse, Response |
10 | 10 | from psycopg_pool import AsyncConnectionPool
|
11 | 11 | from app.config import settings
|
12 | 12 | from app.projects import project_routes
|
|
15 | 15 | from app.users import user_routes
|
16 | 16 | from app.tasks import task_routes
|
17 | 17 | from app.gcp import gcp_routes
|
18 |
| - |
| 18 | +from app.models.enums import HTTPStatus |
| 19 | +from typing import Annotated |
| 20 | +from psycopg import Connection |
| 21 | +from app.db.database import get_db |
19 | 22 |
|
20 | 23 | root = os.path.dirname(os.path.abspath(__file__))
|
21 | 24 | frontend_html = Jinja2Templates(directory="frontend_html")
|
@@ -136,8 +139,22 @@ async def home(request: Request):
|
136 | 139 | return RedirectResponse(f"{settings.API_PREFIX}/docs")
|
137 | 140 |
|
138 | 141 |
|
139 |
| -known_browsers = ["Mozilla", "Chrome", "Safari", "Opera", "Edge", "Firefox"] |
| 142 | +@api.get("/__heartbeat__") |
| 143 | +async def heartbeat_plus_db(db: Annotated[Connection, Depends(get_db)]): |
| 144 | + """Heartbeat that checks that API and DB are both up and running.""" |
| 145 | + try: |
| 146 | + async with db.cursor() as cur: |
| 147 | + await cur.execute("SELECT 1") |
| 148 | + return Response(status_code=HTTPStatus.OK) |
| 149 | + except Exception as e: |
| 150 | + log.warning(e) |
| 151 | + log.warning("Server failed __heartbeat__ database connection check") |
| 152 | + return JSONResponse( |
| 153 | + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, content={"error": str(e)} |
| 154 | + ) |
| 155 | + |
140 | 156 |
|
| 157 | +known_browsers = ["Mozilla", "Chrome", "Safari", "Opera", "Edge", "Firefox"] |
141 | 158 |
|
142 | 159 | @api.exception_handler(404)
|
143 | 160 | async def custom_404_handler(request: Request, _):
|
|
0 commit comments