[][src]Struct tide::Server

pub struct Server<State> { /* fields omitted */ }

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 Tide framework with additional request or response processing, such as compression, default headers, or logging. To add middleware to an app, use the Server::middleware method.

Methods

impl Server<()>[src]

pub fn new() -> Server<()>[src]

Create an empty Server, with no initial middleware or configuration.

impl<State: Send + Sync + 'static> Server<State>[src]

pub fn with_state(state: State) -> Server<State>[src]

Create an Server, with initial middleware or configuration.

pub fn at<'a>(&'a mut self, path: &'a str) -> Route<'a, State>[src]

Add a new route at the given path, relative to root.

Routing means mapping an HTTP request to an endpoint. Here Tide 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. Example:

app.at("/").get(|_| async move {"Hello, world!"});

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.

pub fn middleware(&mut self, m: impl Middleware<State>) -> &mut Self[src]

Add middleware to an application.

Middleware provides application-global 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.

pub fn into_http_service(self) -> Service<State>[src]

Make this app into an HttpService.

This lower-level method lets you host a Tide application within an HTTP server of your choice, via the http_service interface crate.

pub async fn listen(self, addr: impl ToSocketAddrs) -> Result<()>[src]

Asynchronously serve the app at the given address.

Trait Implementations

impl Default for Server<()>[src]

Auto Trait Implementations

impl<State> Send for Server<State> where
    State: Send

impl<State> Sync for Server<State> where
    State: Sync

impl<State> Unpin for Server<State> where
    State: Unpin

impl<State> !UnwindSafe for Server<State>

impl<State> !RefUnwindSafe for Server<State>

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = !

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]