Skip to content

Instantly share code, notes, and snippets.

@s-maj
Created October 21, 2019 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-maj/191e8bb929a42b25edbfd7fad23d8c07 to your computer and use it in GitHub Desktop.
Save s-maj/191e8bb929a42b25edbfd7fad23d8c07 to your computer and use it in GitHub Desktop.
blueprint group
from sanic import Blueprint
from sanic import Sanic
from sanic.response import text
bp1 = Blueprint('bp1', url_prefix='/bp1')
bp2 = Blueprint('bp2', url_prefix='/bp2')
@bp1.middleware('request')
async def bp1_only_middleware(request):
print('applied on Blueprint : bp1 Only')
@bp1.route('/')
async def bp1_route(request):
return text('bp1')
@bp2.route('/<param>')
async def bp2_route(request, param):
return text(param)
group = Blueprint.group(bp1, bp2)
@group.middleware('request')
async def group_middleware(request):
print('common middleware applied for both bp1 and bp2')
app = Sanic(__name__)
app.blueprints(group)
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment