Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named middleware doesn't work #3004

Closed
1 task done
atticusfyj opened this issue Oct 23, 2024 · 2 comments
Closed
1 task done

Named middleware doesn't work #3004

atticusfyj opened this issue Oct 23, 2024 · 2 comments
Labels

Comments

@atticusfyj
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Middlewares registered with app.register_named_middleware don't seem to work.

Code snippet

# playground.py
from sanic import Sanic, text

app = Sanic("my-app")

@app.get("/test", name="test")
def test(request):
    return text("This is response")

async def middleware(request):
    print("This is middleware")

app.register_named_middleware(middleware, ["test"])

Expected Behavior

Running sanic playground:app --host=0.0.0.0 --port=8000 --workers=1 and initiate a GET to 0.0.0.0:8000 is expected to emit a print in console.

How do you run Sanic?

Sanic CLI

Operating System

MacOS

Sanic Version

24.6.0

Additional context

No response

@atticusfyj atticusfyj added the bug label Oct 23, 2024
@cosmastech
Copy link

I think you need to add @app.on_request to your middleware function.

@app.on_request
async def middleware(request):
    print("This is middleware")

@ahopkins
Copy link
Member

ahopkins commented Dec 12, 2024

You need to use the full route name:

app.register_named_middleware(middleware, ["my-app.test"])

For reference, this is how Blueprint-based middleware works.

@ahopkins ahopkins closed this as not planned Won't fix, can't repro, duplicate, stale Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants