Allow insertion of routes with higher priority than existing ones.
See original GitHub issueIs your feature related to a problem? Please describe.
It would be helpful to have a clean way to insert new routes and mounts at the front of the route list. Specifically, if one has a Starlette app created by a library and that app includes a catch-all route (i.e. "/{path:path}"
) then currently it is impossible to add any more specific routes without delving into the internals of the router (e.g. app.routes.insert(0, new_route)
).
An example of a situation where this is a problem is when trying to add additional routes to an application created with JustPy. JustPy creates its Starlette app, complete with a catch-all route, when the library is imported, making it impossible to add an API for the app without direct manipulation of the route table.
Describe the solution you would like.
Ideally Router.add_route()
and Router.mount()
(and possibly their corresponding reflections in the Starlette
class) should have a flag, defaulting to False
, to indicate that the route should be added with highest priority instead of lowest priority.
Describe alternatives you considered
One alternative to expanding the API would be to explicitly document that direct manipulation of the list value in the Starlette.routes
property is allowed. Doing this currently works, but it depends on the current implementation of the routing system and will break if that design were to change significantly. Adding the flag to the mount()
and add_route()
methods would allow for changes under the hood without breaking code.
Additional context
My use case is specifically to do with trying to add additional routes to an application built with JustPy, but there are likely other libraries that behave in similar ways.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
Well, I guess it’s more about not adding unnecessary complexity.
Also, as you said, it’s a bug on
JustPy
that is in our path here. I guess your initial solution (app.routes.insert(0, new_route)
) should take place here. 🤔I’m closing this issue, but we’re open to reconsideration if someone makes a compelling case.
There’s still no compelling case to reconsider the previous decision.