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
#![doc(html_root_url = "https://docs.rs/gotham/0.5.0")]
#![warn(missing_docs, deprecated)]
#![cfg_attr(feature = "ci", deny(warnings))]
#![cfg_attr(
feature = "cargo-clippy",
allow(
clippy::needless_lifetimes,
clippy::should_implement_trait,
clippy::unit_arg,
clippy::match_wild_err_arm,
clippy::new_without_default,
clippy::wrong_self_convention,
clippy::mutex_atomic,
clippy::borrowed_box,
clippy::get_unwrap,
)
)]
#![doc(test(no_crate_inject, attr(deny(warnings))))]
#![deny(private_in_public)]
pub mod extractor;
pub mod handler;
pub mod helpers;
pub mod middleware;
pub mod pipeline;
pub mod router;
mod service;
pub mod state;
pub mod test;
pub mod plain;
#[cfg(feature = "rustls")]
pub mod tls;
pub use anyhow;
pub use hyper;
#[cfg(feature = "rustls")]
pub use tokio_rustls::rustls;
use futures::prelude::*;
use hyper::server::conn::Http;
use std::net::ToSocketAddrs;
use std::sync::Arc;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::{TcpListener, TcpStream};
use tokio::runtime::{self, Runtime};
use crate::{handler::NewHandler, service::GothamService};
pub use plain::*;
#[cfg(feature = "rustls")]
pub use tls::start as start_with_tls;
fn new_runtime(threads: usize) -> Runtime {
runtime::Builder::new()
.threaded_scheduler()
.core_threads(threads)
.thread_name("gotham-worker")
.enable_all()
.build()
.unwrap()
}
async fn tcp_listener<A>(addr: A) -> std::io::Result<TcpListener>
where
A: ToSocketAddrs + 'static,
{
let addr = addr
.to_socket_addrs()
.expect("unable to parse listener address")
.next()
.expect("unable to resolve listener address");
TcpListener::bind(addr).await
}
pub async fn bind_server<'a, NH, F, Wrapped, Wrap>(
mut listener: TcpListener,
new_handler: NH,
wrap: Wrap,
) -> Result<(), ()>
where
NH: NewHandler + 'static,
F: Future<Output = Result<Wrapped, ()>> + Unpin + Send + 'static,
Wrapped: Unpin + AsyncRead + AsyncWrite + Send + 'static,
Wrap: Fn(TcpStream) -> F,
{
let protocol = Arc::new(Http::new());
let gotham_service = GothamService::new(new_handler);
listener
.incoming()
.filter_map(|sock| match sock {
Ok(sock) => future::ready(Some(sock)),
Err(e) => {
panic!("socket error = {:?}", e);
}
})
.for_each(|socket| {
let addr = socket.peer_addr().unwrap();
let service = gotham_service.connect(addr);
let accepted_protocol = protocol.clone();
let wrapper = wrap(socket);
let task = async move {
let socket = wrapper.await?;
accepted_protocol
.serve_connection(socket, service)
.with_upgrades()
.map_err(|_| ())
.await?;
Result::<_, ()>::Ok(())
};
tokio::spawn(task);
future::ready(())
})
.await;
Ok(())
}