poem icon indicating copy to clipboard operation
poem copied to clipboard

how to create fn middleware with state?

Open geekflyer opened this issue 2 years ago • 1 comments

Is there a way to create a fn middleware with state? Something similar to https://docs.rs/axum/latest/axum/middleware/fn.from_fn_with_state.html . Use case is a custom middleware that does some auth checking and needs access to a database.

I know I can use .data to add state to a handler but the same technique doesn't appear to work for middleware fns.

geekflyer avatar May 22 '23 09:05 geekflyer

You can get it from request with .extensions().get() one you add it to the router. https://docs.rs/poem/1.3.55/poem/struct.Request.html#method.extensions

VersBinarii avatar Jun 03 '23 06:06 VersBinarii