From 3e048e9675199f70c1ae1fb99f30c059fd183711 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Sun, 2 Jan 2022 01:21:16 -0800 Subject: [PATCH] change(python): centralize router inclusion Now, when we want to add, remove routes, our base routes should be defined in aurweb.routers.__init__.APP_ROUTES. Signed-off-by: Kevin Morris --- aurweb/asgi.py | 22 ++++++++-------------- aurweb/routers/__init__.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/aurweb/asgi.py b/aurweb/asgi.py index 15c2d40a..161f6f26 100644 --- a/aurweb/asgi.py +++ b/aurweb/asgi.py @@ -17,11 +17,12 @@ from starlette.middleware.sessions import SessionMiddleware import aurweb.config import aurweb.logging +from aurweb import prometheus, util from aurweb.auth import BasicAuthBackend from aurweb.db import get_engine, query from aurweb.models import AcceptedTerm, Term -from aurweb.prometheus import http_api_requests_total, http_requests_total, instrumentator -from aurweb.routers import accounts, auth, html, packages, pkgbase, requests, rpc, rss, sso, trusted_user +from aurweb.prometheus import instrumentator +from aurweb.routers import APP_ROUTES from aurweb.templates import make_context, render_template # Setup the FastAPI app. @@ -29,8 +30,8 @@ app = FastAPI() # Instrument routes with the prometheus-fastapi-instrumentator # library with custom collectors and expose /metrics. -instrumentator().add(http_api_requests_total()) -instrumentator().add(http_requests_total()) +instrumentator().add(prometheus.http_api_requests_total()) +instrumentator().add(prometheus.http_requests_total()) instrumentator().instrument(app) @@ -74,16 +75,9 @@ async def app_startup(): app.add_middleware(SessionMiddleware, secret_key=session_secret) # Add application routes. - app.include_router(sso.router) - app.include_router(html.router) - app.include_router(auth.router) - app.include_router(accounts.router) - app.include_router(trusted_user.router) - app.include_router(rss.router) - app.include_router(packages.router) - app.include_router(pkgbase.router) - app.include_router(requests.router) - app.include_router(rpc.router) + def add_router(module): + app.include_router(module.router) + util.apply_all(APP_ROUTES, add_router) # Initialize the database engine and ORM. get_engine() diff --git a/aurweb/routers/__init__.py b/aurweb/routers/__init__.py index 35d43c03..da79e38f 100644 --- a/aurweb/routers/__init__.py +++ b/aurweb/routers/__init__.py @@ -3,3 +3,22 @@ API routers for FastAPI. See https://fastapi.tiangolo.com/tutorial/bigger-applications/ """ +from . import accounts, auth, html, packages, pkgbase, requests, rpc, rss, sso, trusted_user + +""" +aurweb application routes. This constant can be any iterable +and each element must have a .router attribute which points +to a fastapi.APIRouter. +""" +APP_ROUTES = [ + accounts, + auth, + html, + packages, + pkgbase, + requests, + trusted_user, + rss, + rpc, + sso, +]