pub struct Server { /* private fields */ }Expand description
An HTTP/1.1 server. Configure it, then call serve from
each worker of a giong runtime.
use std::time::Duration;
use giong::net::TcpListener;
use giong_http::{Request, Response, Server};
async fn hello(req: Request) -> Response {
match (req.method(), req.path()) {
("GET", "/") => Response::text("hello"),
_ => Response::new(404),
}
}
fn main() -> std::io::Result<()> {
giong::Builder::new()
.shutdown_on_signals(true)
.shutdown_grace(Duration::from_secs(10))
.build()?
.run(|_worker| async {
let listener = TcpListener::bind("0.0.0.0:8080").expect("bind");
Server::new().serve(listener, hello).await;
})
}Implementations§
Source§impl Server
impl Server
Sourcepub fn idle_timeout(self, timeout: Duration) -> Self
pub fn idle_timeout(self, timeout: Duration) -> Self
How long a keep-alive connection may sit between requests before it is closed (default: 60 s).
Sourcepub fn request_timeout(self, timeout: Duration) -> Self
pub fn request_timeout(self, timeout: Duration) -> Self
How long a client may take to send one complete request, counted from
the moment the server first has to wait for more of it (default: 30 s).
Slower clients get 408 Request Timeout.
Sourcepub fn write_timeout(self, timeout: Duration) -> Self
pub fn write_timeout(self, timeout: Duration) -> Self
How long a client may take to accept a batch of responses before the connection is dropped (default: 30 s).
Sourcepub fn max_head_size(self, bytes: usize) -> Self
pub fn max_head_size(self, bytes: usize) -> Self
Largest accepted request head, in bytes (default: 64 KiB). Larger heads
get 431 Request Header Fields Too Large.
Sourcepub fn max_body_size(self, bytes: usize) -> Self
pub fn max_body_size(self, bytes: usize) -> Self
Largest accepted request body, in bytes (default: 1 MiB). Larger bodies
get 413 Content Too Large.
Sourcepub fn shutdown_linger(self, linger: Duration) -> Self
pub fn shutdown_linger(self, linger: Duration) -> Self
After a shutdown is requested, how long the server keeps picking up
connections and requests that were already on their way (default:
100 ms). Requests in progress are not affected by this: they get the
runtime’s shutdown_grace.
Sourcepub async fn serve<H, Fut>(&self, listener: TcpListener, handler: H)
pub async fn serve<H, Fut>(&self, listener: TcpListener, handler: H)
Accepts connections on listener and answers their requests with
handler until the runtime is asked to shut down.
Each connection runs as its own task on the current worker. When a
shutdown is requested, the listener stops accepting, idle connections
are closed, and requests being handled are finished and answered with
Connection: close, within the grace period configured on the runtime.
A handler that panics takes down its connection only.
Sourcepub async fn serve_unix<H, Fut>(&self, listener: UnixListener, handler: H)
pub async fn serve_unix<H, Fut>(&self, listener: UnixListener, handler: H)
Like serve, on a Unix domain socket: the usual way to
sit behind a reverse proxy on the same machine.
let listener = UnixListener::bind("/run/app.sock")?;
Server::new().serve_unix(listener, handler).await;Sourcepub async fn serve_tls<H, Fut>(
&self,
listener: TcpListener,
acceptor: TlsAcceptor,
handler: H,
)
pub async fn serve_tls<H, Fut>( &self, listener: TcpListener, acceptor: TlsAcceptor, handler: H, )
Like serve, over TLS: every accepted connection first
completes a TLS handshake with acceptor.
let acceptor = giong_tls::TlsAcceptor::from_pem(
&std::fs::read("cert.pem")?,
&std::fs::read("key.pem")?,
)?;
let listener = TcpListener::bind("0.0.0.0:8443")?;
Server::new().serve_tls(listener, acceptor, handler).await;Sourcepub async fn serve_connection<T, H, Fut>(&self, stream: T, handler: Rc<H>)
pub async fn serve_connection<T, H, Fut>(&self, stream: T, handler: Rc<H>)
Serves a single, already established connection until it is closed. For servers that run their own accept loop or their own transport.