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
use crate::{CloneCounter, Server};
use std::marker::PhantomData;
use trillium::Handler;
use trillium_http::Stopper;
use trillium_tls_common::Acceptor;
#[derive(Debug)]
pub struct Config<ServerType, AcceptorType> {
pub(crate) acceptor: AcceptorType,
pub(crate) port: Option<u16>,
pub(crate) host: Option<String>,
pub(crate) nodelay: bool,
pub(crate) stopper: Stopper,
pub(crate) counter: CloneCounter,
pub(crate) register_signals: bool,
server: PhantomData<ServerType>,
}
impl<ServerType, AcceptorType> Config<ServerType, AcceptorType>
where
ServerType: Server,
AcceptorType: Acceptor<ServerType::Transport>,
{
pub fn run<H: Handler>(self, h: H) {
ServerType::run(self, h)
}
pub async fn run_async(self, handler: impl Handler) {
ServerType::run_async(self, handler).await
}
pub fn with_port(mut self, port: u16) -> Self {
self.port = Some(port);
self
}
pub fn with_host(mut self, host: &str) -> Self {
self.host = Some(host.into());
self
}
pub fn without_signals(mut self) -> Self {
self.register_signals = false;
self
}
pub fn with_nodelay(mut self) -> Self {
self.nodelay = true;
self
}
pub fn with_acceptor<A: Acceptor<ServerType::Transport>>(
self,
acceptor: A,
) -> Config<ServerType, A> {
Config {
acceptor,
host: self.host,
port: self.port,
nodelay: self.nodelay,
server: PhantomData,
stopper: self.stopper,
counter: self.counter,
register_signals: self.register_signals,
}
}
pub fn with_stopper(mut self, stopper: Stopper) -> Self {
self.stopper = stopper;
self
}
}
impl<ServerType> Config<ServerType, ()> {
pub fn new() -> Self {
Self::default()
}
}
impl<ServerType, AcceptorType: Clone> Clone for Config<ServerType, AcceptorType> {
fn clone(&self) -> Self {
Self {
acceptor: self.acceptor.clone(),
port: self.port,
host: self.host.clone(),
server: PhantomData,
nodelay: self.nodelay,
stopper: self.stopper.clone(),
counter: self.counter.clone(),
register_signals: self.register_signals,
}
}
}
impl<ServerType> Default for Config<ServerType, ()> {
fn default() -> Self {
Self {
acceptor: (),
port: None,
host: None,
server: PhantomData,
nodelay: false,
stopper: Stopper::new(),
counter: CloneCounter::new(),
register_signals: cfg!(unix),
}
}
}