Skip to main content

Running

Struct Running 

Source
pub struct Running { /* private fields */ }
Expand description

A bound, running server.

Implementations§

Source§

impl Running

Source

pub fn addr(&self) -> SocketAddr

Examples found in repository?
examples/hello.rs (line 64)
29fn main() {
30    let mut config = Config::from_env("APP_");
31    config.set("service.name", "hello");
32
33    let mut router = Router::new();
34
35    router.get("/hello/:name", |req| {
36        Response::text(format!("hello {}", req.param("name").unwrap_or("world")))
37    });
38
39    router.post("/echo", |req| match req.json() {
40        Ok(value) => Response::json(&value),
41        Err(err) => Response::error(Status::BAD_REQUEST, &err.to_string()),
42    });
43
44    router.get("/search", |req| {
45        let query = req.query_param("q").unwrap_or_default();
46        Response::json(&Json::object(vec![("query", Json::from(query))]))
47    });
48
49    // Host is configurable so the same binary works on a laptop and in a
50    // container, where binding to 127.0.0.1 would be unreachable from outside.
51    let address = format!(
52        "{}:{}",
53        config.get_or("host", "127.0.0.1"),
54        config.int("port").unwrap_or(8080)
55    );
56
57    let server = Server::new(router)
58        .config(config)
59        .with(RequestLog)
60        .plugin(Health)
61        .bind(&address)
62        .expect("bind");
63
64    println!("listening on http://{}", server.addr());
65    server.wait();
66}
Source

pub fn port(&self) -> u16

Source

pub fn shutdown_handle(&self) -> Shutdown

A stop switch that can be moved elsewhere, for a signal handler.

Source

pub fn wait(self)

Block until the server stops. Without a Shutdown held somewhere else, this blocks for the life of the process, which is what a plain server wants.

Examples found in repository?
examples/hello.rs (line 65)
29fn main() {
30    let mut config = Config::from_env("APP_");
31    config.set("service.name", "hello");
32
33    let mut router = Router::new();
34
35    router.get("/hello/:name", |req| {
36        Response::text(format!("hello {}", req.param("name").unwrap_or("world")))
37    });
38
39    router.post("/echo", |req| match req.json() {
40        Ok(value) => Response::json(&value),
41        Err(err) => Response::error(Status::BAD_REQUEST, &err.to_string()),
42    });
43
44    router.get("/search", |req| {
45        let query = req.query_param("q").unwrap_or_default();
46        Response::json(&Json::object(vec![("query", Json::from(query))]))
47    });
48
49    // Host is configurable so the same binary works on a laptop and in a
50    // container, where binding to 127.0.0.1 would be unreachable from outside.
51    let address = format!(
52        "{}:{}",
53        config.get_or("host", "127.0.0.1"),
54        config.int("port").unwrap_or(8080)
55    );
56
57    let server = Server::new(router)
58        .config(config)
59        .with(RequestLog)
60        .plugin(Health)
61        .bind(&address)
62        .expect("bind");
63
64    println!("listening on http://{}", server.addr());
65    server.wait();
66}
Source

pub fn shutdown(self)

Stop accepting and wait for in-flight requests to finish.

Trait Implementations§

Source§

impl Drop for Running

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.