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
use futures_util::future::{MapErr, TryFutureExt};
use log::{error, info};
use std::net::ToSocketAddrs;
use std::sync::Arc;
use tokio::net::TcpStream;
use tokio_rustls::{rustls, Accept, TlsAcceptor};
use super::handler::NewHandler;
use super::{bind_server, new_runtime, tcp_listener, StartError};
#[cfg(feature = "testing")]
pub mod test;
pub fn start<NH, A>(
addr: A,
new_handler: NH,
tls_config: rustls::ServerConfig,
) -> Result<(), StartError>
where
NH: NewHandler + 'static,
A: ToSocketAddrs + 'static + Send,
{
start_with_num_threads(addr, new_handler, tls_config, num_cpus::get())
}
pub fn start_with_num_threads<NH, A>(
addr: A,
new_handler: NH,
tls_config: rustls::ServerConfig,
threads: usize,
) -> Result<(), StartError>
where
NH: NewHandler + 'static,
A: ToSocketAddrs + 'static + Send,
{
let runtime = new_runtime(threads);
runtime.block_on(init_server(addr, new_handler, tls_config))
}
pub async fn init_server<NH, A>(
addr: A,
new_handler: NH,
tls_config: rustls::ServerConfig,
) -> Result<(), StartError>
where
NH: NewHandler + 'static,
A: ToSocketAddrs + 'static + Send,
{
let listener = tcp_listener(addr).await?;
let addr = listener.local_addr().unwrap();
info! {
target: "gotham::start",
" Gotham listening on http://{}", addr
}
let wrap = rustls_wrap(tls_config);
bind_server(listener, new_handler, wrap).await
}
pub(crate) fn rustls_wrap(
tls_config: rustls::ServerConfig,
) -> impl Fn(TcpStream) -> MapErr<Accept<TcpStream>, fn(std::io::Error) -> ()> {
fn log_error(error: std::io::Error) {
error!(target: "gotham::tls", "TLS handshake error: {:?}", error);
}
let tls = TlsAcceptor::from(Arc::new(tls_config));
move |socket| tls.accept(socket).map_err(log_error)
}