2

I've been trying to compile the SayHi middleware from the example listed in Actix-web's documentation: https://actix.rs/docs/middleware/. The problem is that in the latest version of Actix-web (3.3.2) and Rust (1.55) this code does not compile, the templates are missing arguments (presumably because the request type has moved to them?), even if I move the request type to the correct position other stuff breaks.

How does one implement a simple middleware like the SayHi example in the latest version of actix-web?

I am relatively new to Rust so please bare with me if I am missing something obvious, but I have been at this for two days now and I cannot seem be able to find or make any code that successfully creates a simple actix-web middleware in the latest versions.

This is the example code from the actix documentation as of October 16th 2021:

use std::pin::Pin;
use std::task::{Context, Poll};

use actix_service::{Service, Transform};
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
use futures::future::{ok, Ready};
use futures::Future;

pub struct SayHi;

impl<S, B> Transform<S> for SayHi
where
    S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
    S::Future: 'static,
    B: 'static,
{
    type Request = ServiceRequest;
    type Response = ServiceResponse<B>;
    type Error = Error;
    type InitError = ();
    type Transform = SayHiMiddleware<S>;
    type Future = Ready<Result<Self::Transform, Self::InitError>>;

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

pub struct SayHiMiddleware<S> {
    service: S,
}

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

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.service.poll_ready(cx)
    }

    fn call(&mut self, req: ServiceRequest) -> Self::Future {
        println!("Hi from start. You requested: {}", req.path());

        let fut = self.service.call(req);

        Box::pin(async move {
            let res = fut.await?;

            println!("Hi from response");
            Ok(res)
        })
    }
}
Matt
  • 699
  • 5
  • 15

1 Answers1

3

Well it turns out the solution was rather trivial indeed.

Rather than importing the definitions for Service and Transform from the actix-service, which has outdated (or newer?) definitions, they should be imported straight from the actix_web submodule dev, like this:

use std::pin::Pin;
use std::task::{Context, Poll};

// use actix_service::{Service, Transform};
use actix_web::dev::{ServiceRequest, ServiceResponse, Service, Transform};
use actix_web::Error;
use futures::future::{ok, Ready};
use futures::Future;
Matt
  • 699
  • 5
  • 15