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 futures::prelude::*;
use log::info;
use std::net::ToSocketAddrs;
use super::handler::NewHandler;
use super::{bind_server, new_runtime, tcp_listener};
pub mod test;
pub fn start<NH, A>(addr: A, new_handler: NH)
where
NH: NewHandler + 'static,
A: ToSocketAddrs + 'static + Send,
{
start_with_num_threads(addr, new_handler, num_cpus::get())
}
pub fn start_with_num_threads<NH, A>(addr: A, new_handler: NH, threads: usize)
where
NH: NewHandler + 'static,
A: ToSocketAddrs + 'static + Send,
{
let mut runtime = new_runtime(threads);
let _ = runtime.block_on(async { init_server(addr, new_handler).await });
}
pub async fn init_server<NH, A>(addr: A, new_handler: NH) -> Result<(), ()>
where
NH: NewHandler + 'static,
A: ToSocketAddrs + 'static + Send,
{
let listener = tcp_listener(addr).map_err(|_| ()).await?;
let addr = listener.local_addr().unwrap();
info!(
target: "gotham::start",
" Gotham listening on http://{}",
addr
);
bind_server(listener, new_handler, future::ok).await
}