actix / examples

Community showcase and examples of Actix Web ecosystem usage.
Apache License 2.0
3.7k stars 807 forks source link

Add example showing how to access shared state from middleware in 1.0.0-alpha.6 #113

Closed ctsrc closed 5 years ago

ctsrc commented 5 years ago

Accessing shared state in handlers is demonstrated in https://github.com/actix/examples/blob/815e34f32ded77f73145fb0373a060b22636f689/state/src/main.rs

And with the way middleware worked previously, you could access shared state in the middleware as such: https://stackoverflow.com/questions/50428385/application-state-access-from-an-actix-web-application-middleware/50428541#50428541

But with the way middleware works now, as demonstrated in https://github.com/actix/examples/blob/09b0188ff99e872898793bd815382a4105219b77/middleware/src/redirect.rs. it is not apparent how one would access shared state from middleware.

Adding an example showing how to access shared state from middleware in 1.0.0-alpha.6 would be immensly helpful.

ctsrc commented 5 years ago

Got an answer to this on Reddit from one of the people involved with actix. It's quite simple so no example for this is needed after all I think.

https://docs.rs/actix-web/1.0.0-alpha.6/actix_web/dev/struct.ServiceRequest.html#method.app_data