pub struct Server<A = DefaultAcceptor> { /* private fields */ }
Expand description
Represents an HTTP server with customization capabilities for handling incoming requests.
Implementations§
Source§impl Server
impl Server
Sourcepub fn bind(addr: SocketAddr) -> Self
pub fn bind(addr: SocketAddr) -> Self
Constructs a server bound to the provided address.
Sourcepub fn from_tcp(listener: TcpListener) -> Self
pub fn from_tcp(listener: TcpListener) -> Self
Constructs a server from an existing std::net::TcpListener
.
Source§impl<A> Server<A>
impl<A> Server<A>
Sourcepub fn acceptor<Acceptor>(self, acceptor: Acceptor) -> Server<Acceptor>
pub fn acceptor<Acceptor>(self, acceptor: Acceptor) -> Server<Acceptor>
Replace the current acceptor with a new one.
Examples found in repository?
17async fn main() {
18 let app = Router::new().route("/", get(handler));
19
20 let config = RustlsConfig::from_pem_file(
21 "examples/self-signed-certs/cert.pem",
22 "examples/self-signed-certs/key.pem",
23 )
24 .await
25 .unwrap();
26
27 let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
28
29 println!("listening on {}", addr);
30
31 let acceptor = CustomAcceptor::new(RustlsAcceptor::new(config));
32 let server = hyper_server::bind(addr).acceptor(acceptor);
33
34 server.serve(app.into_make_service()).await.unwrap();
35}
Sourcepub fn enable_proxy_protocol(
self,
parsing_timeout: Option<Duration>,
) -> Server<ProxyProtocolAcceptor<A>>
pub fn enable_proxy_protocol( self, parsing_timeout: Option<Duration>, ) -> Server<ProxyProtocolAcceptor<A>>
Enable proxy protocol header parsing. Note has to be called after initial acceptor is set.
Sourcepub fn map<Acceptor, F>(self, acceptor: F) -> Server<Acceptor>where
F: FnOnce(A) -> Acceptor,
pub fn map<Acceptor, F>(self, acceptor: F) -> Server<Acceptor>where
F: FnOnce(A) -> Acceptor,
Maps the current acceptor to a new type.
Sourcepub fn handle(self, handle: Handle) -> Self
pub fn handle(self, handle: Handle) -> Self
Provides the server with a handle for extra utilities.
Examples found in repository?
13async fn main() {
14 let app = Router::new().route("/", get(|| async { "Hello, world!" }));
15
16 let handle = Handle::new();
17
18 // Spawn a task to shutdown server.
19 tokio::spawn(shutdown(handle.clone()));
20
21 let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
22 println!("listening on {}", addr);
23 hyper_server::bind(addr)
24 .handle(handle)
25 .serve(app.into_make_service())
26 .await
27 .unwrap();
28
29 println!("server is shut down");
30}
More examples
16async fn main() {
17 let app = Router::new().route("/", get(|| async { "Hello, world!" }));
18
19 let handle = Handle::new();
20
21 // Spawn a task to gracefully shutdown server.
22 tokio::spawn(graceful_shutdown(handle.clone()));
23
24 let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
25 println!("listening on {}", addr);
26 hyper_server::bind(addr)
27 .handle(handle)
28 .serve(app.into_make_service())
29 .await
30 .unwrap();
31
32 println!("server is shut down");
33}
Sourcepub fn http_config(self, config: HttpConfig) -> Self
pub fn http_config(self, config: HttpConfig) -> Self
Replaces the current HTTP configuration.
Examples found in repository?
10async fn main() {
11 let app = Router::new().route("/", get(|| async { "Hello, world!" }));
12
13 let config = HttpConfig::new()
14 .http1_only(true)
15 .http2_only(false)
16 .max_buf_size(8192)
17 .build();
18
19 let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
20 println!("listening on {}", addr);
21 hyper_server::bind(addr)
22 .http_config(config)
23 .serve(app.into_make_service())
24 .await
25 .unwrap();
26}
Sourcepub fn addr_incoming_config(self, config: AddrIncomingConfig) -> Self
pub fn addr_incoming_config(self, config: AddrIncomingConfig) -> Self
Replaces the current incoming address configuration.
Examples found in repository?
11async fn main() {
12 let app = Router::new().route("/", get(|| async { "Hello, world!" }));
13
14 let config = AddrIncomingConfig::new()
15 .tcp_nodelay(true)
16 .tcp_sleep_on_accept_errors(true)
17 .tcp_keepalive(Some(Duration::from_secs(32)))
18 .tcp_keepalive_interval(Some(Duration::from_secs(1)))
19 .tcp_keepalive_retries(Some(1))
20 .build();
21
22 let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
23 println!("listening on {}", addr);
24 hyper_server::bind(addr)
25 .addr_incoming_config(config)
26 .serve(app.into_make_service())
27 .await
28 .unwrap();
29}
Sourcepub async fn serve<M>(self, make_service: M) -> Result<()>where
M: MakeServiceRef<AddrStream, Request<Body>>,
A: Accept<AddrStream, M::Service> + Clone + Send + Sync + 'static,
A::Stream: AsyncRead + AsyncWrite + Unpin + Send,
A::Service: SendService<Request<Body>> + Send,
A::Future: Send,
pub async fn serve<M>(self, make_service: M) -> Result<()>where
M: MakeServiceRef<AddrStream, Request<Body>>,
A: Accept<AddrStream, M::Service> + Clone + Send + Sync + 'static,
A::Stream: AsyncRead + AsyncWrite + Unpin + Send,
A::Service: SendService<Request<Body>> + Send,
A::Future: Send,
Serves the provided MakeService
.
The MakeService
is responsible for constructing services for each incoming connection.
Each service is then used to handle requests from that specific connection.
§Arguments
make_service
: A mutable reference to a type implementing theMakeServiceRef
trait. This will be used to produce a service for each incoming connection.
§Errors
This method can return errors in the following scenarios:
- When binding to an address fails.
- If the
make_service
function encounters an error during itspoll_ready
call. It’s worth noting that this error scenario doesn’t typically occur withaxum
make services.
Examples found in repository?
More examples
19async fn http_server() {
20 let app = Router::new().route("/", get(http_handler));
21
22 let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
23 println!("http listening on {}", addr);
24 hyper_server::bind(addr)
25 .serve(app.into_make_service())
26 .await
27 .unwrap();
28}
29
30async fn http_handler(uri: Uri) -> Redirect {
31 let uri = format!("https://127.0.0.1:3443{}", uri.path());
32
33 Redirect::temporary(&uri)
34}
35
36async fn https_server() {
37 let app = Router::new().route("/", get(|| async { "Hello, world!" }));
38
39 let config = RustlsConfig::from_pem_file(
40 "examples/self-signed-certs/cert.pem",
41 "examples/self-signed-certs/key.pem",
42 )
43 .await
44 .unwrap();
45
46 let addr = SocketAddr::from(([127, 0, 0, 1], 3443));
47 println!("https listening on {}", addr);
48 hyper_server::bind_rustls(addr, config)
49 .serve(app.into_make_service())
50 .await
51 .unwrap();
52}
10async fn main() {
11 let app = Router::new().route("/", get(|| async { "Hello, world!" }));
12
13 let config = HttpConfig::new()
14 .http1_only(true)
15 .http2_only(false)
16 .max_buf_size(8192)
17 .build();
18
19 let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
20 println!("listening on {}", addr);
21 hyper_server::bind(addr)
22 .http_config(config)
23 .serve(app.into_make_service())
24 .await
25 .unwrap();
26}