Home

Awesome

asgi-routing

A high performance router written in 🦀 for the ASGI ecosystem. Built on top of routrie and path-tree.

Features

Example

We'll be using Starlette's Response object to handle some ASGI boilerplate that is not relevant to this example.

from starlette.responses import PlainTextResponse

from asgi_routing import Mount, Route, Router


app = Router(
    [
        Route("/", PlainTextResponse("home")),
        Mount(
            "/users",
            Router(
                [
                    Route("/me", PlainTextResponse("me")),
                    Route("/{username}", PlainTextResponse("you")),
                    Route("/{username}/disable", PlainTextResponse("bye")),
                    Route("/nomatch", PlainTextResponse("who?")),
                ]
            ),
        ),
    ]
)

Since this is a pure ASGI router, you can also mount it to a specific path in a Starlette app if you know that that path is high traffic or has a very large routing table. You won't see much of a difference compared to Starlette's built in router unless you have 20+ routes. See benchmarks for more details.