From be4942c1d81af69ff155d77ee2ed91f38ea9e92b Mon Sep 17 00:00:00 2001 From: Xevion Date: Sat, 9 Nov 2024 20:22:18 -0600 Subject: [PATCH] Add misc router for health/migration routes, rename 'authentication' router to 'auth' --- backend/linkpulse/app.py | 26 +++--------------- .../routers/{authentication.py => auth.py} | 0 backend/linkpulse/routers/misc.py | 27 +++++++++++++++++++ backend/linkpulse/tests/test_session.py | 2 +- 4 files changed, 31 insertions(+), 24 deletions(-) rename backend/linkpulse/routers/{authentication.py => auth.py} (100%) create mode 100644 backend/linkpulse/routers/misc.py diff --git a/backend/linkpulse/app.py b/backend/linkpulse/app.py index 76b3d3d..bde4770 100644 --- a/backend/linkpulse/app.py +++ b/backend/linkpulse/app.py @@ -10,7 +10,6 @@ from fastapi import FastAPI from fastapi.responses import ORJSONResponse from fastapi_cache import FastAPICache from fastapi_cache.backends.inmemory import InMemoryBackend -from fastapi_cache.decorator import cache from linkpulse.logging import setup_logging from linkpulse.middleware import LoggingMiddleware from linkpulse.utilities import get_db, is_development @@ -45,10 +44,11 @@ async def lifespan(_: FastAPI) -> AsyncIterator[None]: db.close() -from linkpulse.routers import authentication +from linkpulse.routers import auth, misc app = FastAPI(lifespan=lifespan, default_response_class=ORJSONResponse) -app.include_router(authentication.router) +app.include_router(auth.router) +app.include_router(misc.router) setup_logging() @@ -70,23 +70,3 @@ if is_development: app.add_middleware(LoggingMiddleware) app.add_middleware(CorrelationIdMiddleware) - - -@app.get("/health") -async def health(): - # TODO: Check database connection - return "OK" - - -@app.get("/api/migration") -@cache(expire=60) -async def get_migration(): - """ - Returns the details of the most recent migration. - """ - # Kind of insecure, but this is just a demo thing to show that migratehistory is available. - cursor = db.execute_sql( - "SELECT name, migrated_at FROM migratehistory ORDER BY migrated_at DESC LIMIT 1" - ) - name, migrated_at = cursor.fetchone() - return {"name": name, "migrated_at": migrated_at} diff --git a/backend/linkpulse/routers/authentication.py b/backend/linkpulse/routers/auth.py similarity index 100% rename from backend/linkpulse/routers/authentication.py rename to backend/linkpulse/routers/auth.py diff --git a/backend/linkpulse/routers/misc.py b/backend/linkpulse/routers/misc.py new file mode 100644 index 0000000..5c5b5df --- /dev/null +++ b/backend/linkpulse/routers/misc.py @@ -0,0 +1,27 @@ +from fastapi import APIRouter +from fastapi_cache.decorator import cache +from linkpulse.utilities import get_db + +router = APIRouter() + +db = get_db() + + +@router.get("/health") +async def health(): + # TODO: Check database connection + return "OK" + + +@router.get("/api/migration") +@cache(expire=60) +async def get_migration(): + """ + Returns the details of the most recent migration. + """ + # Kind of insecure, but this is just a demo thing to show that migratehistory is available. + cursor = db.execute_sql( + "SELECT name, migrated_at FROM migratehistory ORDER BY migrated_at DESC LIMIT 1" + ) + name, migrated_at = cursor.fetchone() + return {"name": name, "migrated_at": migrated_at} diff --git a/backend/linkpulse/tests/test_session.py b/backend/linkpulse/tests/test_session.py index b5d1e40..7a246cf 100644 --- a/backend/linkpulse/tests/test_session.py +++ b/backend/linkpulse/tests/test_session.py @@ -6,7 +6,7 @@ from linkpulse.models import Session from linkpulse.tests.random import random_string from linkpulse.tests.test_user import user from linkpulse.utilities import utc_now -from linkpulse.routers.authentication import validate_session +from linkpulse.routers.auth import validate_session from peewee import IntegrityError