Files
romm/backend/utils/router.py
Michael Manganiello 95a66a182e misc: Improve typing for feed schemas
* Add typing for nested objects in Tinfoil and Webrcade feed schemas.
* Do not send Rom background and thumbnail, if not available.
* Correctly build URLs using `starlette` utils.
* Deprecate the `ROMM_HOST` setting, no longer needed.
* Fix FastAPI custom router to prefer routes without trailing slash.
* Fix Webrcade background URL pointing to gallery screenshot.
2024-08-07 22:56:16 -03:00

38 lines
1.3 KiB
Python

from typing import Any, Callable
from fastapi import APIRouter as FastAPIRouter
from fastapi.types import DecoratedCallable
class APIRouter(FastAPIRouter):
"""FastAPI router that automatically adds an alternate route with a trailing slash.
This is needed as FastAPI does not include a built-in way to handle routes with and without
trailing slashes, without requiring a redirect or duplicating the route definition.
Reference: https://github.com/fastapi/fastapi/discussions/7298
"""
def api_route(
self, path: str, *, include_in_schema: bool = True, **kwargs: Any
) -> Callable[[DecoratedCallable], DecoratedCallable]:
if path.endswith("/") and len(path) > 1:
path = path[:-1]
add_path = super().api_route(
path, include_in_schema=include_in_schema, **kwargs
)
alternate_path = path + "/"
add_alternate_path = super().api_route(
alternate_path, include_in_schema=False, **kwargs
)
def decorator(func: DecoratedCallable) -> DecoratedCallable:
# Path without trailing slash is registered first, for router's `url_path_for` to prefer it.
result = add_path(func)
add_alternate_path(func)
return result
return decorator