sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.

Home Page:https://sanic.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Blueprint Middleware runs before App Middleware

simon-lund opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

With the code below, the bp middleware runs before the app/global middleware. Can you explain why?

I expected the middleware to be registered on app level and the bp middleware on bp level
such that the app middleware is always executed first and the bp middleware as soon as the bp is "entered" on a request.

I know that this is theoretically solved by adding priority, but this behavior is not mentioned in the docs and feels like a bug because it contradicts intuition of data flow and locality.

Related Issue: #2636

Code snippet

from sanic import Sanic, Blueprint, text

bp = Blueprint('test')


@bp.get('/')
def hello_world(request):
    return text("Hello World")


@bp.on_request
def bp_middleware(request):
    print("Blueprint Middleware")


app = Sanic(__name__)


@app.on_request
def app_middleware(request):
    print("App Middleware")


app.blueprint(bp)

if __name__ == '__main__':
    app.run("localhost", port=8000, debug=True, dev=True, auto_reload=True)

Expected Behavior

Expected the app middleware to wrap the bp middleware

How do you run Sanic?

As a script (app.run or Sanic.serve)

Operating System

Ubuntu

Sanic Version

22.9.1

Additional context

No response