pub struct Server { /* private fields */ }Expand description
An HTTP server.
Servers are built up as a combination of state, endpoints and middleware:
-
Server state is user-defined, and is provided via the [
Server::with_state] function. The state is available as a shared reference to all app endpoints. -
Endpoints provide the actual application-level code corresponding to particular URLs. The
Server::atmethod creates a new route (using standard router syntax), which can then be used to register endpoints for particular HTTP request types. -
Middleware extends the base Envoy framework with additional request or response processing, such as compression, default headers, or logging. To add middleware to an app, use the
Server::withmethod.
Implementations§
Source§impl Server
impl Server
Sourcepub fn at<'a>(&'a mut self, path: &str) -> Route<'a>
pub fn at<'a>(&'a mut self, path: &str) -> Route<'a>
Add a new route at the given path, relative to root.
Routing means mapping an HTTP request to an endpoint. Here Envoy applies a “table of contents” approach, which makes it easy to see the overall app structure. Endpoints are selected solely by the path and HTTP method of a request: the path determines the resource and the HTTP verb the respective endpoint of the selected resource.
A path is comprised of zero or many segments, i.e. non-empty strings
separated by ‘/’. There are two kinds of segments: concrete and
wildcard. A concrete segment is used to exactly match the respective
part of the path of the incoming request. A wildcard segment on the
other hand extracts and parses the respective part of the path of the
incoming request to pass it along to the endpoint as an argument. A
wildcard segment is written as :name, which creates an endpoint
parameter called name. It is not possible to define wildcard segments
with different names for otherwise identical paths.
Alternatively a wildcard definitions can start with a *, for example
*path, which means that the wildcard will match to the end of given
path, no matter how many segments are left, even nothing.
The name of the parameter can be omitted to define a path that matches
the required structure, but where the parameters are not required.
: will match a segment, and * will match an entire path.
Here are some examples omitting the HTTP verb based endpoint selection:
app.at("/");
app.at("/hello");
app.at("add_two/:num");
app.at("files/:user/*");
app.at("static/*path");
app.at("static/:context/:");There is no fallback route matching, i.e. either a resource is a full match or not, which means that the order of adding resources has no effect.
Sourcepub fn with(&mut self, middleware: impl Middleware + 'static) -> &mut Self
pub fn with(&mut self, middleware: impl Middleware + 'static) -> &mut Self
Add middleware to an application.
Middleware provides customization of the request/response cycle, such as compression,
logging, or header modification. Middleware is invoked when processing a request, and can
either continue processing (possibly modifying the response) or immediately return a
response. See the Middleware trait for details.
Middleware can only be added at the “top level” of an application, and is processed in the order in which it is applied.