New in version 1.5.
Changed in version 2.1.2: Host Matching is now supported. See Host Matching.
In Kyoukai, routes are stored inside a tree structure consisting of multiple Blueprint objects with a parent and children. Each Blueprint contains a group of routes stored on it, which inherit the request hooks and the API prefix of all of its parents.
Blueprints are instantiated similar to app objects, with a name.
my_blueprint = Blueprint("my_blueprint")
Additionally, blueprints take an additional set of parameters which can be used to more finely control the behaviour.
url_prefix
: The URL prefix to add to every request.- For example, if this is set to
/api/v1`, every request attached to this blueprint will only be accessible via ``/api/v1/<route>
.
Blueprints are stored inside a tree structure - that means that all Blueprints have a parent blueprint and 0 to N children blueprints.
Routing with Blueprints is incredibly similar to routing with a bare app object. Internally, an @app.route
maps
to routing on an underlying Blueprint object used as the “root” blueprint.
@my_blueprint.route("/some/route")
async def some_route(ctx: HTTPRequestContext):
return "Some route"
Blueprint.
route
(routing_url, methods=('GET', ), **kwargs)[source]Convenience decorator for adding a route.
This is equivalent to:
route = bp.wrap_route(func, **kwargs)
bp.add_route(route, routing_url, methods)
Error handlers with Blueprints are handled exactly the same as error handlers on bare app objects. The difference between these however is that error handlers are local to the Blueprint and its children.
@my_blueprint.errorhandler(500)
async def e500(ctx: HTTPRequestContext, err: Exception):
return "Handled an error"
If, after creating your blueprint, you attempt to navigate to /some/route
you will find a 404 error living there
instead. This is because you did not register the Blueprint to your application.
app.register_blueprint(my_blueprint)
Internally, this adds a child to the root blueprint, and sets the parent of the child to the root blueprint. If you have a blueprint that you wish to inherit from, you must register your Blueprint as a child of the Blueprint you wish to inherit from.
my_blueprint.add_child(my_other_blueprint)