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

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

Open
sify21 opened this issue Mar 17, 2024 · 1 comment
Open

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

sify21 opened this issue Mar 17, 2024 · 1 comment

Comments

@sify21
Copy link
Contributor

sify21 commented Mar 17, 2024

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:

// You can use the `#[middleware]` macro to add appropriate middleware
// In this case, any `tower::Layer` that takes services of `Request<Body>` will work
#[middleware(crate::middleware::LoggingLayer)]
pub async fn length_of_input(input: String) -> Result<usize, ServerFnError> {
println!("2. Running server function.");
// insert a simulated wait
tokio::time::sleep(std::time::Duration::from_millis(250)).await;
Ok(input.len())
}

use tower::{Layer, Service};
pub struct LoggingLayer;
impl<S> Layer<S> for LoggingLayer {
type Service = LoggingService<S>;
fn layer(&self, inner: S) -> Self::Service {
LoggingService { inner }
}
}
pub struct LoggingService<T> {
inner: T,
}
impl<T> Service<Request<Body>> for LoggingService<T>
where
T: Service<Request<Body>>,
{

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:

  • Redundant routes in actix, since leptos-actix already did this:

// register server functions
for (path, _) in server_fn::actix::server_fn_paths() {
let additional_context = additional_context.clone();
let handler = handle_server_fns_with_context(additional_context);
router = router.route(path, handler);
}

  • It's not easy to apply different middlewares to different server functions

Additional context

Here is how I tried to implement it in my fork
sify21@4381aad

@sify21 sify21 changed the title support actix middleware for server functions support #[middleware] macro on server functions for actix Mar 17, 2024
@gbj
Copy link
Collaborator

gbj commented Mar 18, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants