feature suggestions #1332
-
I would like to thank starlette maintainers for this great library, first. It reminds me of express in many ways and is exactly what I was looking for in the python ecosystem for a long time. It does it's job well and allows to utilize modern python features as well. I would like to provide a few suggestions on the starlette API, which might help improve the developer experience! :) modular apps with routersframeworks like flask make it really easy to make modular applications. Flask has a "Blueprint" abstraction that makes it easier to split routes into multiple files. Starlette already has a router abstraction at it's core, so IMO adding the following methods/ features on it would make it easier to create modular apps. # starlette/routing.py
class Router:
def add_router(router: Router) -> None:
"""Includes the routes in the given router, into the current router instance."""
pass # starlette/applications.py
class Starlette:
def add_router(router: Router) -> None:
"""Includes the routes in the given router, into the app's router instance."""
self.router.add_router(router=router) By adding the ability to add router instances on top of others, we can make our apps modular like: # myapp/main.py
from starlette.applications import Starlette
from myapp.users.routes import user_router
from myapp.notes.routes import note_router
app = Starlette()
app.add_router(user_router)
app.add_router(note_router) # myapp/users/routes.py
from starlette.routing import Router
user_router = Router(prefix="/users")
@user_router.get("")
async def get_users():
pass # myapp/notes/routes.py
from starlette.routing import Router
note_router = Router(prefix="/notes")
@note_router.get("")
async def get_notes():
pass It would also be helpful to have the ability to pass URL prefixes to router instances. I would love to work on these features, and also hear your thoughts/ opinions on this! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Have you tried using Mounts? https://www.starlette.io/routing/#working-with-router-instances |
Beta Was this translation helpful? Give feedback.
Have you tried using Mounts? https://www.starlette.io/routing/#working-with-router-instances