2023-03-01 23:19:10 +00:00
|
|
|
use crate::config::{ApubMiddleware, FederationConfig, RequestData};
|
2023-02-11 12:32:35 +00:00
|
|
|
use axum::{async_trait, body::Body, extract::FromRequestParts, http::Request, response::Response};
|
|
|
|
use http::{request::Parts, StatusCode};
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
use tower::{Layer, Service};
|
|
|
|
|
|
|
|
impl<S, T: Clone> Layer<S> for ApubMiddleware<T> {
|
|
|
|
type Service = ApubService<S, T>;
|
|
|
|
|
|
|
|
fn layer(&self, inner: S) -> Self::Service {
|
|
|
|
ApubService {
|
|
|
|
inner,
|
2023-02-19 12:26:01 +00:00
|
|
|
config: self.0.clone(),
|
2023-02-11 12:32:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 14:18:06 +00:00
|
|
|
/// Passes [FederationConfig] to HTTP handlers, converting it to [RequestData] in the process
|
|
|
|
#[doc(hidden)]
|
2023-02-11 12:32:35 +00:00
|
|
|
#[derive(Clone)]
|
2023-02-19 12:26:01 +00:00
|
|
|
pub struct ApubService<S, T: Clone> {
|
2023-02-11 12:32:35 +00:00
|
|
|
inner: S,
|
2023-02-19 12:26:01 +00:00
|
|
|
config: FederationConfig<T>,
|
2023-02-11 12:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, T> Service<Request<Body>> for ApubService<S, T>
|
|
|
|
where
|
|
|
|
S: Service<Request<Body>, Response = Response> + Send + 'static,
|
|
|
|
S::Future: Send + 'static,
|
|
|
|
T: Clone + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Future = S::Future;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
self.inner.poll_ready(cx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, mut request: Request<Body>) -> Self::Future {
|
2023-02-19 12:26:01 +00:00
|
|
|
request.extensions_mut().insert(self.config.clone());
|
2023-02-11 12:32:35 +00:00
|
|
|
self.inner.call(request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl<S, T: Clone + 'static> FromRequestParts<S> for RequestData<T>
|
|
|
|
where
|
|
|
|
S: Send + Sync,
|
|
|
|
T: Send + Sync,
|
|
|
|
{
|
|
|
|
type Rejection = (StatusCode, &'static str);
|
|
|
|
|
|
|
|
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
2023-02-19 12:26:01 +00:00
|
|
|
match parts.extensions.get::<FederationConfig<T>>() {
|
2023-02-11 12:32:35 +00:00
|
|
|
Some(c) => Ok(c.to_request_data()),
|
|
|
|
None => Err((
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
"Missing extension, did you register ApubMiddleware?",
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|