1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use actix_utils::future::ok;
use actix_web::{
    body::BoxBody,
    dev::{fn_service, Service, ServiceRequest, ServiceResponse},
    http::StatusCode,
    Error, HttpResponseBuilder,
};

/// Creates service that always responds with given status code and echoes request path as response
/// body.
pub fn echo_path_service(
    status_code: StatusCode,
) -> impl Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> {
    fn_service(move |req: ServiceRequest| {
        let path = req.path().to_owned();
        ok(req.into_response(HttpResponseBuilder::new(status_code).body(path)))
    })
}