Skip to main content

Server

Struct Server 

Source
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

Source

pub fn bind(addr: impl ToSocketAddrs) -> Result<Server>

Resolve and remember the listen address(es). Does not bind yet.

Source

pub fn handler<H: Handler + 'static>(self, handler: H) -> Server

Set the request handler.

Source

pub fn handler_arc(self, handler: Arc<dyn Handler>) -> Server

Set the request handler from an existing Arc.

Source

pub fn serve_dir(self, root: impl Into<PathBuf>) -> Server

Serve static files from root (convenience for a StaticFiles handler).

Source

pub fn workers(self, workers: usize) -> Server

Set the number of worker threads for the thread-pool runtime.

Source

pub fn server_name(self, name: Option<String>) -> Server

Set the Server response header value (None to omit it).

Source

pub fn tls(self, acceptor: TlsAcceptor) -> Server

Enable TLS with the given acceptor (turns the server into HTTPS).

Source

pub fn compression(self, options: Options) -> Server

Configure response compression.

Source

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.

Source

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.

Source

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.

Source

pub fn acme(self, manager: AcmeManager) -> Server

Enable automatic certificates via ACME, routed per-connection by SNI. Takes precedence over a static tls acceptor. Currently served by the thread-pool runtime (run).

Source

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.

Source

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.

Source

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.

Source

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]).

Source

pub fn run_mio(self) -> Result<()>

Run on a single-thread mio readiness event loop. Blocks the calling thread.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.