leptos-rs / leptos

Build fast web applications with Rust.
https://leptos.dev
MIT License
15.86k stars 622 forks source link

support #[middleware] macro on server functions for actix #2436

Open sify21 opened 6 months ago

sify21 commented 6 months ago

Is your feature request related to a problem? Please describe.

I want to implement server-side auth with actix framework, which is normally done by middlewares. Currently Leptos supports defining axum middlewares, like in the following example: https://github.com/leptos-rs/leptos/blob/97fd8ff6c46f742ce809398aa05161567b90a16b/examples/server_fns_axum/src/app.rs#L249-L257 https://github.com/leptos-rs/leptos/blob/97fd8ff6c46f742ce809398aa05161567b90a16b/examples/server_fns_axum/src/middleware.rs#L9-L28

I tried to implement the necessary traits in server_fn/src/middleware/mod.rs for actix's middleware to work in the same way, but found it's not possible.

The reason is that actix's middleware, which implements actix_service::Transform trait, is actually a "service-generating factory", and it generates the wrapping Service (or the Transform component) asynchronously. However, Leptos's Layer trait works in synchronous way.

Describe the solution you'd like

Be able to use #[middleware] macros for actix's server functions, just like middlewares for axum's server functions

Describe alternatives you've considered

I can call handle_server_fns() method from leptos-actix, and register the returned Route manually. But this has two drawbacks:

https://github.com/leptos-rs/leptos/blob/97fd8ff6c46f742ce809398aa05161567b90a16b/integrations/actix/src/lib.rs#L1275-L1280

Additional context

Here is how I tried to implement it in my fork https://github.com/sify21/leptos/commit/4381aada4c796a4ee913e8133192f5e404af151d

gbj commented 6 months ago

I spent a while trying to implement this before 0.6, and kept running into "X does not implement Y" trait errors that I wasn't able to figure out, not being familiar at all with Actix middleware. Very open to any changes to server_fns that need to happen to make it work.