Add `routes` argument to Mount (#400)

* Add Mount(routes=...)

* Tweaks
This commit is contained in:
Tom Christie 2019-02-18 18:28:31 +00:00 committed by GitHub
parent 47a810ece8
commit e3543aa08e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 10 deletions

View File

@ -7,6 +7,7 @@ fi
set -x
${PREFIX}mypy starlette --ignore-missing-imports --disallow-untyped-defs
${PREFIX}autoflake --in-place --recursive starlette tests
${PREFIX}black starlette tests
${PREFIX}isort --multi-line=3 --trailing-comma --force-grid-wrap=0 --combine-as --line-width 88 --recursive --apply starlette tests

View File

@ -276,10 +276,23 @@ class WebSocketRoute(BaseRoute):
class Mount(BaseRoute):
def __init__(self, path: str, app: ASGIApp, name: str = None) -> None:
def __init__(
self,
path: str,
app: ASGIApp = None,
routes: typing.List[BaseRoute] = None,
name: str = None,
) -> None:
assert path == "" or path.startswith("/"), "Routed paths must start with '/'"
assert (
app is not None or routes is not None
), "Either 'app', or 'routes' must be specified"
self.path = path.rstrip("/")
self.app = app
if routes is None:
assert app is not None
self.app = app
else:
self.app = Router(routes=routes)
self.name = name
self.path_regex, self.path_format, self.param_convertors = compile_path(
path + "/{path:path}"

View File

@ -38,14 +38,12 @@ app = Router(
Route("/", endpoint=homepage, methods=["GET"]),
Mount(
"/users",
app=Router(
[
Route("/", endpoint=users),
Route("/me", endpoint=user_me),
Route("/{username}", endpoint=user),
Route("/nomatch", endpoint=user_no_match),
]
),
routes=[
Route("/", endpoint=users),
Route("/me", endpoint=user_me),
Route("/{username}", endpoint=user),
Route("/nomatch", endpoint=user_no_match),
],
),
Mount("/static", app=staticfiles),
]