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 <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2022-01-02 01:21:16 -08:00
parent a1f46611e1
commit 3e048e9675
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
2 changed files with 27 additions and 14 deletions

View file

@ -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()

View file

@ -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,
]