logo
pub struct Server<L> { /* private fields */ }
Expand description

HTTP Server

A Server is created to listen on a port, parse HTTP requests, and hand them off to a Service.

Implementations

Create new Server with Listener.

Example

Server::new(TcpListener::bind("0.0.0.0:7878"));

Serve a Service

Try to serve a Service

Serve with graceful shutdown signal.

Example

use salvo_core::prelude::*;

#[fn_handler]
async fn hello_world(res: &mut Response) {
    res.render("Hello World!");
}

#[tokio::main]
async fn main() {
    let (tx, rx) = oneshot::channel();
    let router = Router::new().get(hello_world);
    let server = Server::new(TcpListener::bind("127.0.0.1:7878")).serve_with_graceful_shutdown(router, async {
        rx.await.ok();
    });

    // Spawn the server into a runtime
    tokio::task::spawn(server);

    // Later, start the shutdown...
    let _ = tx.send(());
}

Serve with graceful shutdown signal.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

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

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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