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

implement middleware chains #828

Open
sansyrox opened this issue Jun 2, 2024 · 2 comments
Open

implement middleware chains #828

sansyrox opened this issue Jun 2, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@sansyrox
Copy link
Member

sansyrox commented Jun 2, 2024

def fx1():
   ...
   
def fx2():
  ...
  
mc = MiddlewareChain(fx1, fx2)

app.before_request("/route", mc)
@ashupednekar
Copy link

@sansyrox Is someone working on this? If not, I'd like to take this up.

Middleware chains are essential for making Robyn more suitable for mainstream production scenarios

@ashupednekar
Copy link

ashupednekar commented Sep 28, 2024

As I understand, rn the limitation stems from

        table.write().unwrap().insert(route.to_string(), function)?;

where the matchit router map for "before/after" request type allows for only one key and panics on duplicate registration.

That's the case of route middlewares, but is handled in case of global middlewares which has a Vec<FunctionInfo>, right?

    for before_middleware in before_middlewares {
        request = match execute_middleware_function(&request, &before_middleware).await {
            Ok(MiddlewareReturn::Request(r)) => r,
            Ok(MiddlewareReturn::Response(r)) => {
                // If a before middleware returns a response, we abort the request and return the response
                return r;
            }
            Err(e) => {
                error!(
                    "Error while executing before middleware function for endpoint `{}`: {}",
                    req.uri().path(),
                    get_traceback(e.downcast_ref::<PyErr>().unwrap())
                );
                return Response::internal_server_error(None);
            }
        };
    }

Speaking of which, I couldn't find documentation on how to add global middlewares from python... the add_global_middleware function is not yet exposed to python, is that it? Sry if I missed sth in the docs xP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants