Server

Struct Server 

Source
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::at method 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::with method.

Implementations§

Source§

impl Server

Source

pub fn new() -> Self

Create a new Envoy server.

Source§

impl Server

Source

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.

Source

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.

Source

pub async fn respond<Req, Res>(self, req: Req) -> Result<Res>
where Req: Into<Request<Body>>, Res: From<Response<Body>>,

Respond to a Request with a Response.

This method is useful for testing endpoints directly, or for creating servers over custom transports.

Source

pub async fn listen(self, addr: SocketAddr) -> Result<(), Error>

Start the server.

Trait Implementations§

Source§

impl Clone for Server

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Server

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Server

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Endpoint for Server

Source§

fn call<'life0, 'life1, 'async_trait>( &'life0 self, ctx: &'life1 mut Context, ) -> Pin<Box<dyn Future<Output = Result> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Invoke the endpoint within the given context

Auto Trait Implementations§

§

impl Freeze for Server

§

impl !RefUnwindSafe for Server

§

impl Send for Server

§

impl Sync for Server

§

impl Unpin for Server

§

impl !UnwindSafe for Server

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more