pub struct Server { /* private fields */ }Expand description
A configured HTTP(S) server, ready to run.
Build it with Server::bind, attach a Handler (or
serve_dir), optionally enable TLS, then call one of
the run* methods for the runtime you compiled in.
Implementations§
Source§impl Server
impl Server
Sourcepub fn bind(addr: impl ToSocketAddrs) -> Result<Server>
pub fn bind(addr: impl ToSocketAddrs) -> Result<Server>
Resolve and remember the listen address(es). Does not bind yet.
Sourcepub fn handler_arc(self, handler: Arc<dyn Handler>) -> Server
pub fn handler_arc(self, handler: Arc<dyn Handler>) -> Server
Set the request handler from an existing Arc.
Sourcepub fn serve_dir(self, root: impl Into<PathBuf>) -> Server
pub fn serve_dir(self, root: impl Into<PathBuf>) -> Server
Serve static files from root (convenience for a StaticFiles handler).
Sourcepub fn workers(self, workers: usize) -> Server
pub fn workers(self, workers: usize) -> Server
Set the number of worker threads for the thread-pool runtime.
Sourcepub fn server_name(self, name: Option<String>) -> Server
pub fn server_name(self, name: Option<String>) -> Server
Set the Server response header value (None to omit it).
Sourcepub fn tls(self, acceptor: TlsAcceptor) -> Server
pub fn tls(self, acceptor: TlsAcceptor) -> Server
Enable TLS with the given acceptor (turns the server into HTTPS).
Sourcepub fn compression(self, options: Options) -> Server
pub fn compression(self, options: Options) -> Server
Configure response compression.
Sourcepub fn hsts(self, value: Option<String>) -> Server
pub fn hsts(self, value: Option<String>) -> Server
Set the Strict-Transport-Security header value sent on secure
responses (e.g. "max-age=31536000"), or None to omit it. Never sent
over plain HTTP.
Sourcepub fn allow_http(self, allow: bool) -> Server
pub fn allow_http(self, allow: bool) -> Server
Serve content over plain HTTP instead of redirecting to HTTPS. Off by default — this server upgrades HTTP requests to HTTPS.
Sourcepub fn http_redirect(self, addr: impl ToSocketAddrs) -> Result<Server>
pub fn http_redirect(self, addr: impl ToSocketAddrs) -> Result<Server>
Also bind a plain-HTTP listener (e.g. port 80) that redirects to HTTPS
and serves ACME HTTP-01 challenges. Runs on its own thread under
run.
Sourcepub fn acme(self, manager: AcmeManager) -> Server
pub fn acme(self, manager: AcmeManager) -> Server
Sourcepub fn alt_svc(self, value: Option<String>) -> Server
pub fn alt_svc(self, value: Option<String>) -> Server
Set the Alt-Svc header value (e.g. advertising HTTP/3) sent on
responses, or None to omit it.
Sourcepub fn notify_bound(self, tx: Sender<()>) -> Server
pub fn notify_bound(self, tx: Sender<()>) -> Server
Register a notifier fired exactly once, after every listener this server
owns has been bound, just before it starts serving. This lets an external
coordinator wait until all privileged binds are done before dropping
privileges. The TCP runtime (run) signals once both the
main listener and any HTTP redirect listener are bound; the HTTP/3
runtime (run_h3) signals once its UDP socket is bound.
Sourcepub fn run(self) -> Result<()>
pub fn run(self) -> Result<()>
Run on the blocking thread-pool runtime. Blocks the calling thread. If an HTTP redirect listener is configured, it runs on its own thread.
Sourcepub async fn run_tokio(self) -> Result<()>
pub async fn run_tokio(self) -> Result<()>
Run on a tokio runtime. Requires being called from within a tokio
runtime context (e.g. under #[tokio::main]).
Sourcepub fn run_mio(self) -> Result<()>
pub fn run_mio(self) -> Result<()>
Run on a single-thread mio readiness event loop. Blocks the calling thread.
Sourcepub fn run_h3(self) -> Result<()>
pub fn run_h3(self) -> Result<()>
Run an HTTP/3 server on a QUIC/UDP event loop, listening on the same address(es) as the TCP server (but over UDP). HTTP/3 is always encrypted.
Under ACME, certificates are selected per-connection by peeking the SNI
from the QUIC Initial (purecrypto::quic::peek_initial_sni); the QUIC
loop serves already-issued certs (the TCP path does the issuing). With a
static tls acceptor, that one certificate is used.
Blocks the calling thread.