Skip to main content

Components injection, fast routing and non-global (layered) middlewares. Give your Sanic application a Boom!

Project description

Components injection, fast routing and non-global (layered) middlewares. Give your Sanic application a Boom!

In a nutshell

"""Example code taken from

import datetime as dt
import inspect
import typing as t

from marshmallow import Schema, fields, post_load
from sanic.exceptions import ServerError
from sanic.response import text

from sanic_boom import Component, SanicBoom

# --------------------------------------------------------------------------- #
# marshmallow related code
# --------------------------------------------------------------------------- #

class User(object):
    def __init__(self, name, email): = name = email
        self.created_at =

    def __repr__(self):
        return "<User(name={!r})>".format(self=self)

    def say_hi(self):
        return "hi, my name is {}".format(

class UserSchema(Schema):
    name = fields.Str()
    email = fields.Email()
    created_at = fields.DateTime()

    def make_user(self, data):
        return User(**data)

# --------------------------------------------------------------------------- #
# sanic-boom related code
# --------------------------------------------------------------------------- #

class JSONBody(t.Generic[t.T_co]):

class JSONBodyComponent(Component):
    def resolve(self, param: inspect.Parameter) -> bool:
        if hasattr(param.annotation, "__origin__"):
            return param.annotation.__origin__ == JSONBody
        return False

    async def get(self, request, param: inspect.Parameter) -> object:
        inferred_type = param.annotation.__args__[0]
            return inferred_type().load(request.json).data
        except Exception:
            raise ServerError(
                "Couldn't convert JSON body to {!s}".format(inferred_type)

app = SanicBoom(__name__)
async def handler(user: JSONBody[UserSchema]):  # notice the handler parameters
    return text(user.say_hi())

if __name__ == "__main__":"", port=8000, workers=1)
$ curl -v http://localhost:8000/ -d '{"name":"John Doe","email":"john.doe@example.tld"}'
*   Trying ::1...
* connect to ::1 port 8000 failed: Connection refused
*   Trying
* Connected to localhost ( port 8000 (#0)
> POST / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.61.1
> Accept: */*
> Content-Length: 50
> Content-Type: application/x-www-form-urlencoded
* upload completely sent off: 50 out of 50 bytes
< HTTP/1.1 200 OK
< Connection: keep-alive
< Keep-Alive: 5
< Content-Length: 23
< Content-Type: text/plain; charset=utf-8
* Connection #0 to host localhost left intact
hi, my name is John Doe


IMPORTANT: sanic-boom is in very early stages of development! Use with caution and be aware that some functionalities and APIs may change between versions until they’re out of alpha.


sanic-boom depends on two “not-so-known” libraries (both created by the author of sanic-boom):


Since xrtr replaces the Sanic default router under the hood in sanic-boom, it is very important for the developer to read its documentation (in the link provided above).


sanic-boom is a free software distributed under the MIT license.


v0.1.1 on 2018-10-18

  • Fixed a bug where handlers derived from HTTPMethodView class were not being executed (for their signature actually be *args, **kwargs).

v0.1.0 on 2018-10-17

  • First release on PyPI. (Probably) not stable.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for sanic-boom, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size sanic_boom-0.1.1-py3-none-any.whl (14.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size sanic-boom-0.1.1.tar.gz (30.2 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page