Closed tonychengchime closed 2 years ago
Hi @tonychengchime, thanks for reaching out.
Yes! Twirp has support for both "middlewares" and hooks. They're nothing like the ones you see in Starlette though. And for your use case, they're not ideal.
For more nuanced use cases, twirp has the concept of hooks. This is how it looks like in go: https://pkg.go.dev/github.com/twitchtv/twirp#ServerHooks
Equivalent implementation in python. https://github.com/verloop/twirpy/blob/0.0.7/twirp/hook.py
You can pass a hook with the kwarg like so, TwirpAsgiApp(hook=MyHook)
.
Since you want to do auth, you can use request_received
or request_routed
hook. Both of them run before the request body is read at all.
You can raise a TwirpServerException
with Unauthenticated
or PermissionDenied
code from within your hook.
I can post a more complete example tomorrow if that'll help.
Hi Piyush, that was very helpful. I got what I need so I will close this ticket. Thanks
Today I got some time to check starlette's middlewares again.
@tonychengchime check the "Using middleware in other frameworks" section on https://www.starlette.io/middleware/
Twirpy is a standard ASGI app, so it should work for youad well.
Hi, does Twirpy support Middlewares like Starlette? I'm trying to implement an auth layer and would like to do it in a Middleware.
I did not see in the documentation or code where to specify them.
Thanks