use std::{
future::{ready, Future, Ready},
pin::Pin,
};
use actix_web::body::BoxBody;
use actix_web::{
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
http, Error, HttpResponse,
};
#[derive(Default)]
pub struct StripAppRoot {}
impl StripAppRoot {
pub fn new() -> Self {
Self {}
}
}
impl<S, B> Transform<S, ServiceRequest> for StripAppRoot
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: actix_web::body::MessageBody,
B: 'static,
{
type Response = ServiceResponse<BoxBody>;
type Error = Error;
type InitError = ();
type Transform = StripAppRootMiddleware<S>;
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ready(Ok(StripAppRootMiddleware { service }))
}
}
pub struct StripAppRootMiddleware<S> {
service: S,
}
type LocalBoxFuture<T> = Pin<Box<dyn Future<Output = T> + 'static>>;
impl<S, B> Service<ServiceRequest> for StripAppRootMiddleware<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
B: actix_web::body::MessageBody,
{
type Response = ServiceResponse<BoxBody>;
type Error = Error;
type Future = LocalBoxFuture<Result<Self::Response, Self::Error>>;
forward_ready!(service);
fn call(&self, mut req: ServiceRequest) -> Self::Future {
let prefix = clean_prefix();
if let Some(path) = req.uri().path_and_query() {
let old_uri = req.uri();
if let Some(stripped) = path.as_str().strip_prefix(&prefix) {
let new_path = if stripped.is_empty() { "/" } else { stripped };
let mut builder = http::Uri::builder();
if let Some(auth) = old_uri.authority() {
builder = builder.authority(auth.clone());
}
if let Some(s) = old_uri.scheme() {
builder = builder.scheme(s.clone());
}
builder = builder.path_and_query(new_path);
let new_uri = builder.build().unwrap();
let head = req.head_mut();
head.uri = new_uri.clone();
req.match_info_mut().get_mut().update(&new_uri);
let fut = self.service.call(req);
return Box::pin(async move {
let res = fut.await?;
Ok(res.map_into_boxed_body())
});
}
}
Box::pin(async move {
let (req, _pl) = req.into_parts();
let res = HttpResponse::NotFound().finish();
Ok(ServiceResponse::new(req, res))
})
}
}
fn clean_prefix() -> String {
let mut prefix = crate::app_root();
if !prefix.starts_with('/') {
prefix.insert(0, '/');
}
if prefix.len() > 1 && prefix.ends_with('/') {
prefix.pop();
}
prefix
}