1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
use super::{Node, NodeContext, NodeKind, NodeRequest, NodeResponse};
use crate::error::ServiceError;
use anyhow::Result;
use futures::Future;
use http_body_util::BodyExt;
use hyper::{header::HeaderValue, Response, StatusCode};
use serde::Deserialize;
use std::{pin::Pin, sync::Arc};
pub struct RedirectKind;
#[derive(Deserialize)]
pub struct Redirect(HeaderValueWrap);
impl NodeKind for RedirectKind {
fn name(&self) -> &'static str {
"redirect"
}
fn instanciate(&self, config: serde_yml::Value) -> Result<Arc<dyn Node>> {
Ok(Arc::new(serde_yml::from_value::<Redirect>(config)?))
}
}
impl Node for Redirect {
fn handle<'a>(
&'a self,
_context: &'a mut NodeContext,
_request: NodeRequest,
) -> Pin<Box<dyn Future<Output = Result<NodeResponse, ServiceError>> + Send + Sync + 'a>> {
Box::pin(async move {
let mut resp = Response::new("".to_string()).map(|b| b.map_err(|e| match e {}).boxed());
*resp.status_mut() = StatusCode::MOVED_PERMANENTLY;
resp.headers_mut().insert("Location", self.0 .0.clone());
Ok(resp)
})
}
}
struct HeaderValueWrap(HeaderValue);
impl<'de> Deserialize<'de> for HeaderValueWrap {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Ok(HeaderValueWrap(
HeaderValue::from_str(&s).map_err(|e| serde::de::Error::custom(format!("{e}")))?,
))
}
}
|