Skip to content

adriangb/asgi-routing

Repository files navigation

asgi-routing

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

Features

  • Pure ASGI, compatible with any ASGI web framework.
  • Very fast, we use a radix-tree router written in Rust.
  • Support for all of the common routing primitives like path parameters and catch-all parameters.

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published