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
51
|
use super::{Node, NodeContext, NodeKind, NodeRequest, NodeResponse};
use crate::{config::DynNode, error::ServiceError};
use futures::Future;
use hyper::header::HOST;
use serde::Deserialize;
use serde_yml::Value;
use std::{collections::HashMap, pin::Pin, sync::Arc};
#[derive(Deserialize)]
#[serde(transparent)]
struct Hosts(HashMap<String, DynNode>);
pub struct HostsKind;
impl NodeKind for HostsKind {
fn name(&self) -> &'static str {
"hosts"
}
fn instanciate(&self, config: Value) -> anyhow::Result<Arc<dyn Node>> {
Ok(Arc::new(serde_yml::from_value::<Hosts>(config)?))
}
}
impl Node for Hosts {
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 host = request
.headers()
.get(HOST)
.and_then(|e| e.to_str().ok())
.map(remove_port);
let node = match host {
Some(host) => self
.0
.get(host)
.or_else(|| self.0.get(":fallback"))
.ok_or(ServiceError::UnknownHost)?,
None => self.0.get(":none").ok_or(ServiceError::NoHost)?,
};
node.handle(context, request).await
})
}
}
pub fn remove_port(s: &str) -> &str {
s.split_once(":").map(|(s, _)| s).unwrap_or(s)
}
|