I am trying to register multiple actix web middleware like this
app.wrap(cors)
.wrap(
Logger,
)
.app_data(dataA)
.app_data(dataB)
.service(handlerA)
.service(handlerB)
.service(
web::scope("")
.wrap(Middleware1 {})
.wrap(Middleware2 {})
.service(handler1)
.service(handler2)
)
But I realise that Middleware1
is never executed on requests, but Middleware2
is. But when I remove Middleware2
, then Middleware1
executes.
So it seems somehow only the last middleware is registered. This is obviously not want I want.
So the question is, how do I register multiple middlewares correctly?
As per the documentation
Warning: if you use wrap() or wrap_fn() multiple times, the last occurrence will be executed first.
Based on that, it is likely both of your middleware are executed, not in the order you would expect. If the second middleware somehow rely on the result of the first one, then you need to change their order.
You can try with middlewares that simply log something to the console to see their order of execution.