pub struct Running { /* private fields */ }Expand description
A bound, running server.
Implementations§
Source§impl Running
impl Running
Sourcepub fn addr(&self) -> SocketAddr
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}pub fn port(&self) -> u16
Sourcepub fn shutdown_handle(&self) -> Shutdown
pub fn shutdown_handle(&self) -> Shutdown
A stop switch that can be moved elsewhere, for a signal handler.
Sourcepub fn wait(self)
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}Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Running
impl !UnwindSafe for Running
impl Freeze for Running
impl Send for Running
impl Sync for Running
impl Unpin for Running
impl UnsafeUnpin for Running
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more