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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use async_trait::async_trait;
use fnv::FnvHashMap;
use tokio::sync::Mutex;
use urlpattern::UrlPatternMatchInput;
use core::fmt;
use std::{
    io,
    net::{SocketAddr, ToSocketAddrs},
    path::PathBuf, pin::Pin, sync::Arc, any::{Any, TypeId}, ops::Deref, fmt::Formatter,
};

use hyper::Body;

use crate::{server::{util::{get_certs_and_key, process_result}, http_server::{launch_http_server, HttpServerOpt}, https_server::{launch_https_server, HttpsServerOpt}, http3_server::{launch_http3_server, Http3ServerOpt}}, process::save_pid, resource::{Resource, Responder}};


#[derive(Default)]
pub struct Data (FnvHashMap<TypeId, Box<dyn Any + Sync + Send>>);

impl Deref for Data {
    type Target = FnvHashMap<TypeId, Box<dyn Any + Sync + Send>>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Data {
    pub fn new(data: FnvHashMap<TypeId, Box<dyn Any + Sync + Send>>) -> Self {
        Self(data)
    }

    pub fn default() -> Self {
        Self(FnvHashMap::default())
    }

    pub fn insert<D>(&mut self, data: D)
    where
        D: Any + Send + Sync,
    {
        self.0.insert(TypeId::of::<D>(), Box::new(data));
    }
}

impl fmt::Debug for Data {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.debug_tuple("Data").finish()
    }
}

pub type Result<T, E = ChiteyError> = std::result::Result<T, E>;

pub struct ContextImpl {
    pub(crate) data: Data,
}

impl ContextImpl {
    pub fn new() -> Self {
        ContextImpl { data: Data::new(FnvHashMap::default()) }
    }

    #[must_use]
    pub fn insert<D>(mut self, data: D) -> Self
    where
        D: Any + Send + Sync,
    {
        self.data.insert(data);
        self
    }
}

#[derive(Clone)]
pub struct Context {
    data: Arc<ContextImpl>,
}

impl Context {
    pub fn new(data: ContextImpl) -> Self {
        Self {
            data: Arc::new(data),
        }
    }

    pub fn get<D>(&self) -> &D
    where
        D: Any + Send + Sync,
    {
        self.data_opt::<D>().unwrap_or_else(|| panic!("Data `{}` does not exist.", std::any::type_name::<D>()))
    }

    pub fn data_opt<D>(&self) -> Option<&D>
    where
        D: Any + Send + Sync,
    {
        self.data.data.get(&TypeId::of::<D>()).and_then(|d| d.downcast_ref::<D>())
    }
}

#[derive(Clone)]
pub struct Factories {
    pub(crate) factories: Vec<(Arc<Resource>, Arc<Mutex<Pin<Box<dyn HttpServiceFactory + 'static + Send + Sync>>>>)>,
    pub(crate) contexts: Context,
}
unsafe impl Send for Factories {}
unsafe impl Sync for Factories {}

#[derive(Clone)]
pub struct Certs {
    pub cert: PathBuf,
    pub key: PathBuf,
}

#[async_trait]
pub trait HttpServiceFactory: Sync
{
    fn register(&self) -> Resource;
    fn analyze_types(&self, url: UrlPatternMatchInput) -> bool;
    async fn handler_func(&self, url: UrlPatternMatchInput, req: Request) -> Responder;
}

pub struct WebServer {
    cert: Option<Certs>,
    listen: Option<SocketAddr>,
    tls_listen: Option<SocketAddr>,
    redirect: Option<String>,
    factories: Vec<(Resource, Pin<Box<dyn HttpServiceFactory + 'static + Send + Sync>>)>,
    data: Data,
}

impl WebServer
{
    pub fn new() -> Self {
        Self {
            cert: None,
            listen: None,
            tls_listen: None,
            redirect: None,
            factories: Vec::new(),
            data: Data::default(),
        }
    }

    pub fn service<F>(mut self, factory: F) -> Self
    where
        F: HttpServiceFactory + 'static + Send + Sync,
    {
        let resource = factory.register();
        self.factories.push((resource, Box::pin(factory)));
        self
    }

    pub fn bind<A>(mut self, address: A) -> io::Result<Self>
    where
        A: ToSocketAddrs,
    {
        match address.to_socket_addrs() {
            Ok(v) => {
                for addr in v.collect::<Vec<SocketAddr>>() {
                    self.listen = Some(addr);
                }
            }
            Err(e) => return Err(e),
        };

        Ok(self)
    }

    pub fn tls_bind<A>(mut self, address: A) -> io::Result<Self>
    where
        A: ToSocketAddrs,
    {
        match address.to_socket_addrs() {
            Ok(v) => {
                for addr in v.collect::<Vec<SocketAddr>>() {
                    self.tls_listen = Some(addr);
                }
            }
            Err(e) => return Err(e),
        };

        Ok(self)
    }

    pub fn redirect<U: std::ops::Deref<Target=str>>(mut self, url: U) -> Self {
        self.redirect = Some(url.to_string());
        self
    }

    pub fn tls(mut self, cert: Certs) -> Self {
        self.cert = Some(cert);
        self
    }

    pub async fn run(self) -> Result<(), ChiteyError> {
        let mut factories = Vec::new();
        let mut factories2 = Vec::new();
        for factory in self.factories {
            let (res, fact) = factory;
            let fac = Arc::new(Mutex::new(fact));
            factories.push((Arc::new(res.clone()), fac.clone()));
            factories2.push((Arc::new(res), fac.clone()));
        }

        let contexts = Context::new(ContextImpl { data: self.data });
        let factories = Factories{ factories, contexts: contexts.clone() };
        let factories2 = Factories{ factories: factories2, contexts: contexts.clone() };

        if let Some(cert) = self.cert {
            let tls_certs_key = match get_certs_and_key(cert) {
                Ok(v) => v,
                Err(e) => return Err(ChiteyError::KeyAnalyzeError(e.to_string())),
            };
            let tls_certs_key2 = tls_certs_key.clone();
            let handle_http = async {
                let factories = factories.clone();
                if let Some(li) = self.listen {
                    let http_server_opt = HttpServerOpt{ listen: li, redirect: self.redirect };
                    let _ = tokio::spawn(async move {
                        loop {
                            process_result(launch_http_server(http_server_opt.clone(), save_pid, factories.clone()).await);
                        };
                    }).await;
                }
            };
            let factories = factories.clone();
            let handle_https = async {
                if let Some(li) = self.tls_listen {
                    let https_server_opt = HttpsServerOpt{listen: li};
                    let http3_server_opt = Http3ServerOpt{listen: li};
                    let handle_https = tokio::spawn(async move {
                        loop {
                            process_result(launch_https_server(tls_certs_key.clone(), https_server_opt.clone(), factories.clone()).await);
                        }
                    });
                    let handle_http3 = tokio::spawn(async move {
                        loop {
                            process_result(launch_http3_server(tls_certs_key2.clone(), http3_server_opt.clone(), factories2.clone()).await);
                        }
                    });
                    let (_, _) = tokio::join!(handle_https, handle_http3);
                }
            };

            let (_, _) = tokio::join!(
                handle_http,
                handle_https,
            );
        };

        eprintln!("You must set key always!! Right or Fake or not!!");

        Ok(())
    }

    #[must_use]
    pub fn data<D>(mut self, data: D) -> Self
    where
        D: Any + Send + Sync
    {
        self.data.insert(data);
        self
    }
}

pub type Request = (http::Request<Body>, bool, Context);

use thiserror::Error;
#[derive(Error, Debug)]
pub enum ChiteyError {
    #[error("extract value failed")]
    UrlPatternError,
    #[error("server failed: {0}")]
    InternalServerError(String),
    #[error("cannot analyze key: {0}")]
    KeyAnalyzeError(String),
    #[error("failed kill server: {0}")]
    ServerKillError(String),
}