Asoke: a simple dispatcher for ASGI apps using Hypercorn and Starlette

July 6, 2022
Pai Nai

Prerequisites

For years I loved CherryPy dispatching, the approach originating from Plone, as I remember. However, I dislike its complexity.

I dislike Flask and Routes, as well as all other frameworks.

I never understood REST, from its very beginning. Especially for APIs. I had to follow this approach at work, but I dislike it.

What could be simpler than a mapping {urlpath: handler}? So an app would look like this:

async def app(scope, receive, send):
    handler = handlers[scope['path']]
    await handler(scope, receive, send)

and handlers would look as if it was a CherryPy application:

class Entities:

    @expose
    async def get(scope, receive, send):
        # ...

    @expose
    async def put(scope, receive, send)
        # ...

class App:
    entities = Entities()

oh, we need a mapping...

handlers = make_handlers(App)

Or, a quickstart, as in CherryPy, would be even better:

if __name__ == '__main__':
    quickstart(App(), {{'bind': '127.0.0.1:12345'}})

Implementation

Despite I dislike most mainstream frameworks, I use Request and various response objects from Starlette. I don't dislike it yet. Among others, EventSourceResponse from Starlette is the best, IMHO.

So, here it is. It's not an artwork yet, just a palette. Or, deep alpha. Suitable for API only for now.