aboutsummaryrefslogtreecommitdiff
path: root/src/modules/mod.rs
blob: 0fe9ca0dbc2f2b65e1a320a2ddb941ec9a0b784b (plain)
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
52
53
54
55
56
57
58
59
60
use crate::error::ServiceError;
use crate::State;
use accesslog::AccessLogKind;
use auth::{basic::HttpBasicAuthKind, cookie::CookieAuthKind};
use bytes::Bytes;
use error::ErrorKind;
use file::FileKind;
use files::FilesKind;
use futures::Future;
use headers::HeadersKind;
use hosts::HostsKind;
use http_body_util::combinators::BoxBody;
use hyper::{body::Incoming, Request, Response};
use proxy::ProxyKind;
use serde_yaml::Value;
use std::{net::SocketAddr, pin::Pin, sync::Arc};
use switch::SwitchKind;

pub mod accesslog;
pub mod auth;
pub mod error;
pub mod file;
pub mod files;
pub mod headers;
pub mod hosts;
pub mod proxy;
pub mod switch;

pub type NodeRequest = Request<Incoming>;
pub type NodeResponse = Response<BoxBody<Bytes, ServiceError>>;

pub static MODULES: &[&dyn NodeKind] = &[
    &HttpBasicAuthKind,
    &CookieAuthKind,
    &ProxyKind,
    &HostsKind,
    &FilesKind,
    &FileKind,
    &AccessLogKind,
    &ErrorKind,
    &HeadersKind,
    &SwitchKind,
];

pub struct NodeContext {
    pub state: Arc<State>,
    pub addr: SocketAddr,
}

pub trait NodeKind: Send + Sync + 'static {
    fn name(&self) -> &'static str;
    fn instanciate(&self, config: Value) -> anyhow::Result<Arc<dyn Node>>;
}
pub trait Node: Send + Sync + 'static {
    fn handle<'a>(
        &'a self,
        context: &'a mut NodeContext,
        request: NodeRequest,
    ) -> Pin<Box<dyn Future<Output = Result<NodeResponse, ServiceError>> + Send + Sync + 'a>>;
}