-
-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* cleanup * refactor * simplified design * fix ci * finished taversal * simplified implementation * added docs * Update starlite/asgi/routing_trie/validate.py Co-authored-by: provinzkraut <[email protected]> * Update starlite/router.py Co-authored-by: provinzkraut <[email protected]> * Update starlite/router.py Co-authored-by: provinzkraut <[email protected]> * addressed revoew comments * 1.35.0 Co-authored-by: provinzkraut <[email protected]>
- Loading branch information
1 parent
c308fea
commit de3bb1d
Showing
45 changed files
with
1,117 additions
and
824 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Mounting ASGI Apps | ||
|
||
Starlite support "mounting" ASGI applications on sub paths, that is - specifying a handler function that will handle all | ||
requests addressed to a given path. | ||
|
||
```py title="Mounting an ASGI App" | ||
--8<-- "examples/routing/mount_custom_app.py" | ||
``` | ||
|
||
The handler function will receive all requests with a url that begins with `/some/sub-path`, e.g. `/some/sub-path` and | ||
`/some/sub-path/abc` and `/some/sub-path/123/another/sub-path` etc. | ||
|
||
!!! info Technical Details | ||
If we were to send a request to the above with the url `/some/sub-path/abc`, the handler will be invoked and | ||
the value of `scope["path"]` will equal `/`. If we send a request to `/some/sub-path/abc`, it will also be invoked, | ||
and `scope["path"]` will equal `/abc`. | ||
|
||
Mounting is especially useful when you need to combine components of other ASGI applications - for example, for 3rd part libraries. | ||
The following example is identical in principle to the one above but it uses `Starlette`: | ||
|
||
```py title="Mounting a Starlette App" | ||
--8<-- "examples/routing/mounting_starlette_app.py" | ||
``` |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
from starlite import Response, Starlite, asgi | ||
|
||
if TYPE_CHECKING: | ||
from starlite.types import Receive, Scope, Send | ||
|
||
|
||
@asgi("/some/sub-path", is_mount=True) | ||
async def my_asgi_app(scope: "Scope", receive: "Receive", send: "Send") -> None: | ||
""" | ||
Args: | ||
scope: The ASGI connection scope. | ||
receive: The ASGI receive function. | ||
send: The ASGI send function. | ||
Returns: | ||
None | ||
""" | ||
response = Response(content={"forwarded_path": scope["path"]}) | ||
await response(scope, receive, send) | ||
|
||
|
||
app = Starlite(route_handlers=[my_asgi_app]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
from starlette.applications import Starlette | ||
from starlette.responses import JSONResponse | ||
from starlette.routing import Route | ||
|
||
from starlite import Starlite, asgi | ||
|
||
if TYPE_CHECKING: | ||
from starlette.requests import Request | ||
|
||
|
||
async def index(request: "Request") -> JSONResponse: | ||
"""A generic starlette handler.""" | ||
return JSONResponse({"forwarded_path": request.url.path}) | ||
|
||
|
||
starlette_app = asgi(path="/some/sub-path", is_mount=True)( | ||
Starlette( | ||
debug=True, | ||
routes=[ | ||
Route("/", index), | ||
Route("/abc", index), | ||
Route("/123/another/sub-path", index), | ||
], | ||
) | ||
) | ||
|
||
|
||
app = Starlite(route_handlers=[starlette_app]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
|
||
from examples.routing import mount_custom_app, mounting_starlette_app | ||
from starlite.status_codes import HTTP_200_OK | ||
from starlite.testing import TestClient | ||
|
||
if TYPE_CHECKING: | ||
from starlite import Starlite | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"app", | ||
( | ||
mount_custom_app.app, | ||
mounting_starlette_app.app, | ||
), | ||
) | ||
def test_mounting_asgi_app_example(app: "Starlite") -> None: | ||
with TestClient(app) as client: | ||
response = client.get("/some/sub-path") | ||
assert response.status_code == HTTP_200_OK | ||
assert response.json() == {"forwarded_path": "/"} | ||
|
||
response = client.get("/some/sub-path/abc") | ||
assert response.status_code == HTTP_200_OK | ||
assert response.json() == {"forwarded_path": "/abc"} | ||
|
||
response = client.get("/some/sub-path/123/another/sub-path") | ||
assert response.status_code == HTTP_200_OK | ||
assert response.json() == {"forwarded_path": "/123/another/sub-path"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "starlite" | ||
version = "1.34.0" | ||
version = "1.35.0" | ||
description = "Light-weight and flexible ASGI API Framework" | ||
authors = ["Na'aman Hirschfeld <[email protected]>"] | ||
maintainers = [ | ||
|
@@ -110,6 +110,7 @@ disable = [ | |
"cyclic-import", | ||
"duplicate-code", | ||
"fixme", | ||
"import-outside-toplevel", | ||
"line-too-long", | ||
"missing-class-docstring", | ||
"missing-module-docstring", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.