pub struct Server { /* private fields */ }
Expand description
An HTTP server.
use astra::{Body, Request, Response, Server};
Server::bind("localhost:3000")
.serve(|mut req: Request, _info| {
println!("incoming {:?}", req.uri());
Response::new(Body::new("Hello World!"))
})
.expect("failed to start server");
See the crate-level documentation for details.
Implementations§
Source§impl Server
impl Server
Sourcepub fn bind(addr: impl ToSocketAddrs) -> Server
pub fn bind(addr: impl ToSocketAddrs) -> Server
Binds a server to the provided address.
use astra::Server;
use std::net::SocketAddr;
let server = Server::bind("localhost:3000");
let server = Server::bind(SocketAddr::from(([127, 0, 0, 1], 3000)));
§Panics
This method will panic if binding to the address fails.
Sourcepub fn serve<S>(self, service: S) -> Result<()>
pub fn serve<S>(self, service: S) -> Result<()>
Serve incoming connections with the provided service.
use astra::{Body, Request, Response, Server};
Server::bind("localhost:3000")
.serve(|mut req: Request, _| {
println!("incoming {:?}", req.uri());
Response::new(Body::new("Hello World!"))
})
.expect("failed to start server");
Sourcepub fn serve_clone<S>(self, service: S) -> Result<()>
pub fn serve_clone<S>(self, service: S) -> Result<()>
Like Self::serve
but does not wrap service
in an Arc
and expects it to
implement Clone
and Sync
internally.
Sourcepub fn max_workers(self, val: usize) -> Self
pub fn max_workers(self, val: usize) -> Self
Sets the maximum number of threads in the worker pool.
By default, the limit is 15 threads per CPU core.
Sourcepub fn worker_keep_alive(self, val: Duration) -> Self
pub fn worker_keep_alive(self, val: Duration) -> Self
Sets how long to keep alive an idle thread in the worker pool.
By default, the timeout is set to 6 seconds.
Sourcepub fn http1_keep_alive(self, val: bool) -> Self
pub fn http1_keep_alive(self, val: bool) -> Self
Sets whether to use keep-alive for HTTP/1 connections.
Default is true
.
Sourcepub fn http1_half_close(self, val: bool) -> Self
pub fn http1_half_close(self, val: bool) -> Self
Set whether HTTP/1 connections should support half-closures.
Clients can chose to shutdown their write-side while waiting
for the server to respond. Setting this to true
will
prevent closing the connection immediately if read
detects an EOF in the middle of a request.
Default is false
.
Sourcepub fn http1_max_buf_size(self, val: usize) -> Self
pub fn http1_max_buf_size(self, val: usize) -> Self
Set the maximum buffer size.
Default is ~ 400kb.
Sourcepub fn http1_pipeline_flush(self, val: bool) -> Self
pub fn http1_pipeline_flush(self, val: bool) -> Self
Sets whether to bunch up HTTP/1 writes until the read buffer is empty.
This isn’t really desirable in most cases, only really being useful in silly pipeline benchmarks.
Sourcepub fn http1_writev(self, enabled: bool) -> Self
pub fn http1_writev(self, enabled: bool) -> Self
Set whether HTTP/1 connections should try to use vectored writes, or always flatten into a single buffer.
Note that setting this to false may mean more copies of body data, but may also improve performance when an IO transport doesn’t support vectored writes well, such as most TLS implementations.
Setting this to true will force hyper to use queued strategy which may eliminate unnecessary cloning on some TLS backends
Default is auto
. In this mode hyper will try to guess which
mode to use
Sourcepub fn http1_title_case_headers(self, val: bool) -> Self
pub fn http1_title_case_headers(self, val: bool) -> Self
Set whether HTTP/1 connections will write header names as title case at the socket level.
Note that this setting does not affect HTTP/2.
Default is false.
Sourcepub fn http1_preserve_header_case(self, val: bool) -> Self
pub fn http1_preserve_header_case(self, val: bool) -> Self
Set whether to support preserving original header cases.
Currently, this will record the original cases received, and store them
in a private extension on the Request
. It will also look for and use
such an extension in any provided Response
.
Since the relevant extension is still private, there is no way to interact with the original cases. The only effect this can have now is to forward the cases in a proxy-like fashion.
Note that this setting does not affect HTTP/2.
Default is false.
Sourcepub fn http1_only(self) -> Self
pub fn http1_only(self) -> Self
Only accepts HTTP/1.
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Get the local address of the bound socket