How to pass data to an actix middleware

I'm trying to learn more about how actix web works and I'm the point where I want to create a middleware. Ultimately I want to create a db connection in the middleware and I'm trying to figure out how to get the connection details to the middleware. I've broken the problem down into a small a sample as I could.

// Data I'd want to share would go here
pub struct Data {
    pub color: String

impl Data {
    pub async fn new() -> Arc<Self> {
        let data = Data { color: "red".to_string() };


pub struct MyData;

impl<S, B> Transform<S, ServiceRequest> for MyData
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
    S::Future: 'static,
    B: 'static,
    type Response = ServiceResponse<B>;
    type Error = Error;
    type InitError = ();
    type Transform = MyDataMiddleware<S>;
    type Future = Ready<Result<Self::Transform, Self::InitError>>;

    fn new_transform(&self, service: S) -> Self::Future {
        ok(MyDataMiddleware { service })

pub struct MyDataMiddleware<S> {
    service: S,

impl<S, B> Service<ServiceRequest> for MyDataMiddleware<S>
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
    S::Future: 'static,
    B: 'static,
    type Response = ServiceResponse<B>;
    type Error = Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;


    fn call(&self, req: ServiceRequest) -> Self::Future {
        println!("In middleware");
        let app_data = req.app_data::<actix_web::web::Data<Data>>();
        // thread 'actix-rt|system:0|arbiter:0' panicked at 'called `Option::unwrap()` on a `None` value'
        let state = app_data.as_ref().unwrap();
        let color = state.as_ref().color.clone();
        let fut =;

        println!("Color in middleware:{}", color);
        Box::pin(async move {
            let res = match fut.await {
                Ok(response) => response,
                Err(error) => panic!("Unable to process middleware:{}", error),


async fn main() -> std::io::Result<()> {
    let new_data = Data::new().await;
    let data = actix_web::web::Data::new(new_data);
    let server = match HttpServer::new(move || {
        Ok(value) => value,
        Err(error) => panic!("Error binding to socket:{:?}", error),
# Cargo.toml
name = "actix-middleware"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at

actix = "0.13.0"
actix-web = "4.0.1"
futures = "0.3.21"

I feel like I'm pretty close. Any ideas would be greatly appreciated.

I just figured it out. I was following an example here Triox/ at master · Trioxidation/Triox · GitHub. In the example they get the data with an await and convert it to the proper format.

let app_state = app_state::AppState::new().await;
let app_state = actix_web::web::Data::new(app_state);

I'm guessing this doesn't work for them either due to the .await as the data is probably not ready by the time it is converted to it's proper format. It didn't occur to me until this morning that this might be the problem, as I'm still learning rust and async/await. At any rate, if I refactor without the .await then it starts working!

// let new_data = Data::new().await;
let new_data = Data { color: "red".to_string() };

Well, I've learned even more. While my fix does work, I now understand why the example I was following works and why my implementation of it did not. It has nothing to do with the .await as I had suspected. They created a type that I was missing.

pub type AppData = web::Data<Arc<Data>>;

If you look carefully, you'll notice an Arc in there, that is the key. When I was trying to retrieve my data, I was missing that:

// notice the missing Arc
let app_data = req.app_data::<actix_web::web::Data<Data>>();

The fix then is to create the type and use that to extract the data.

let app_data = req.app_data::<AppData>();