1#[cfg(all(feature = "tls", feature = "stream"))]
2use std::sync::Arc;
3#[cfg(feature = "stream")]
4use std::{io, net::SocketAddr};
5
6use hyper::server::conn::{http1, http2};
7use tower::make::Shared;
8
9use crate::{bridge::rt::TokioExecutor, service::MakeServiceRef};
10
11use super::conn::auto;
12#[cfg(feature = "tls")]
13use super::conn::tls::info::TlsConnectionInfoService;
14#[cfg(feature = "stream")]
15use super::conn::Acceptor;
16use super::conn::MakeServiceConnectionInfoService;
17use super::Accept;
18use super::Server;
19
20#[derive(Debug, Clone, Copy, Default)]
22pub struct NeedsAcceptor {
23 _priv: (),
24}
25
26#[derive(Debug, Clone, Copy, Default)]
28pub struct NeedsProtocol {
29 _priv: (),
30}
31
32#[derive(Debug, Clone, Copy, Default)]
34pub struct NeedsService {
35 _priv: (),
36}
37
38#[derive(Debug, Clone, Copy, Default)]
40pub struct NeedsExecutor {
41 _priv: (),
42}
43
44impl<P, S, B, E> Server<NeedsAcceptor, P, S, B, E> {
45 pub fn with_acceptor<A>(self, acceptor: A) -> Server<A, P, S, B, E>
47 where
48 A: Accept,
49 {
50 Server {
51 acceptor,
52 make_service: self.make_service,
53 protocol: self.protocol,
54 executor: self.executor,
55 body: self.body,
56 }
57 }
58
59 #[cfg(feature = "stream")]
60 pub fn with_incoming<I>(self, incoming: I) -> Server<Acceptor, P, S, B, E>
66 where
67 I: Into<Acceptor> + Into<crate::server::conn::AcceptorCore>,
68 {
69 self.with_acceptor(Acceptor::from(incoming))
70 }
71
72 #[cfg(feature = "stream")]
73 pub async fn with_bind(
75 self,
76 addr: &SocketAddr,
77 ) -> Result<Server<Acceptor, P, S, B, E>, io::Error> {
78 Ok(self.with_acceptor(Acceptor::bind(addr).await?))
79 }
80
81 #[cfg(feature = "stream")]
82 pub async fn with_listener(
84 self,
85 listener: tokio::net::TcpListener,
86 ) -> Server<Acceptor, P, S, B, E> {
87 self.with_acceptor(Acceptor::from(listener))
88 }
89}
90
91impl<A, S, B, E> Server<A, NeedsProtocol, S, B, E> {
92 pub fn with_protocol<P>(self, protocol: P) -> Server<A, P, S, B, E> {
94 Server {
95 acceptor: self.acceptor,
96 make_service: self.make_service,
97 protocol,
98 executor: self.executor,
99 body: self.body,
100 }
101 }
102
103 pub fn with_auto_http(self) -> Server<A, auto::Builder, S, B, E> {
107 self.with_protocol(auto::Builder::default())
108 }
109
110 pub fn with_http1(self) -> Server<A, http1::Builder, S, B, E> {
112 self.with_protocol(http1::Builder::new())
113 }
114
115 pub fn with_http2(self) -> Server<A, http2::Builder<TokioExecutor>, S, B, E> {
117 self.with_protocol(http2::Builder::new(TokioExecutor::new()))
118 }
119}
120
121impl<A, P, B, E> Server<A, P, NeedsService, B, E> {
122 pub fn with_make_service<S>(self, make_service: S) -> Server<A, P, S, B, E> {
130 Server {
131 acceptor: self.acceptor,
132 make_service,
133 protocol: self.protocol,
134 executor: self.executor,
135 body: self.body,
136 }
137 }
138
139 pub fn with_shared_service<S>(self, service: S) -> Server<A, P, Shared<S>, B, E> {
141 Server {
142 acceptor: self.acceptor,
143 make_service: Shared::new(service),
144 protocol: self.protocol,
145 executor: self.executor,
146 body: self.body,
147 }
148 }
149}
150
151impl<A, P, S, B, E> Server<A, P, S, B, E>
152where
153 S: MakeServiceRef<A::Conn, B>,
154 A: Accept,
155{
156 pub fn with_connection_info(self) -> Server<A, P, MakeServiceConnectionInfoService<S>, B, E> {
161 Server {
162 acceptor: self.acceptor,
163 make_service: MakeServiceConnectionInfoService::new(self.make_service),
164 protocol: self.protocol,
165 executor: self.executor,
166 body: self.body,
167 }
168 }
169
170 #[cfg(feature = "tls")]
171 pub fn with_tls_connection_info(self) -> Server<A, P, TlsConnectionInfoService<S>, B, E> {
176 Server {
177 acceptor: self.acceptor,
178 make_service: TlsConnectionInfoService::new(self.make_service),
179 protocol: self.protocol,
180 executor: self.executor,
181 body: self.body,
182 }
183 }
184}
185
186#[cfg(all(feature = "tls", feature = "stream"))]
187impl<P, S, B, E> Server<Acceptor, P, S, B, E> {
188 pub fn with_tls<C>(self, config: C) -> Server<Acceptor, P, S, B, E>
191 where
192 C: Into<Arc<rustls::ServerConfig>>,
193 {
194 Server {
195 acceptor: self.acceptor.with_tls(config.into()),
196 make_service: self.make_service,
197 protocol: self.protocol,
198 executor: self.executor,
199 body: self.body,
200 }
201 }
202}
203
204impl<A, P, S, B> Server<A, P, S, B, NeedsExecutor> {
205 pub fn with_executor<E>(self, executor: E) -> Server<A, P, S, B, E> {
209 Server {
210 acceptor: self.acceptor,
211 make_service: self.make_service,
212 protocol: self.protocol,
213 executor,
214 body: self.body,
215 }
216 }
217
218 pub fn with_tokio(self) -> Server<A, P, S, B, TokioExecutor> {
225 self.with_executor(TokioExecutor::new())
226 }
227}
228
229impl<A, P, S, B, E> Server<A, P, S, B, E> {
230 pub fn with_body<B2>(self) -> Server<A, P, S, B2, E> {
235 Server {
236 acceptor: self.acceptor,
237 make_service: self.make_service,
238 protocol: self.protocol,
239 executor: self.executor,
240 body: Default::default(),
241 }
242 }
243}