From 8c7562b497977e6f8df8d7e55b327e5e31574bea Mon Sep 17 00:00:00 2001 From: Adrian Kappel Date: Tue, 18 Feb 2020 13:21:46 -0500 Subject: [PATCH 1/4] Added example for autoreloading server development --- Cargo.toml | 1 + examples/README.md | 4 ++++ examples/autoreload.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 examples/autoreload.rs diff --git a/Cargo.toml b/Cargo.toml index 7293f62f6..f6225c846 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ pretty_env_logger = "0.3" serde_derive = "1.0" handlebars = "3.0.0" tokio = { version = "0.2", features = ["macros"] } +listenfd = "0.3" [features] default = ["multipart", "websocket"] diff --git a/examples/README.md b/examples/README.md index 21587b769..3f5ecb95e 100644 --- a/examples/README.md +++ b/examples/README.md @@ -35,3 +35,7 @@ Hooray! `warp` also includes built-in support for WebSockets ### TLS - [`tls.rs`](./tls.rs) - can i haz security? + +### Autoreloading + +- [`autoreload.rs`](./autoreload.rs) - Change some code and watch the server reload automatically! diff --git a/examples/autoreload.rs b/examples/autoreload.rs new file mode 100644 index 000000000..80bec4f9c --- /dev/null +++ b/examples/autoreload.rs @@ -0,0 +1,36 @@ +#![deny(warnings)] +use std::convert::Infallible; +use warp::Filter; +use hyper::server::Server; +use listenfd::ListenFd; + +extern crate listenfd; +/// You'll need to install `systemfd` and `cargo-watch`: +/// ``` +/// cargo install systemfd cargo-watch +/// ``` +/// And run with: +/// ``` +/// systemfd --no-pid -s http::3030 -- cargo watch -x 'run --example autoreload' +/// ``` +#[tokio::main] +async fn main() { + // Match any request and return hello world! + let routes = warp::any().map(|| "Hello, World!"); + + // Convert warp filter into a hyper service, allowing us to later use + // it with `hyper::Server::from_tcp(...).serve(make_svc)`. + let svc = warp::service(routes); + let make_svc = hyper::service::make_service_fn(|_: _| async move { + Ok::<_, Infallible>(svc) + }); + + let mut listenfd = ListenFd::from_env(); + let server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() { + Server::from_tcp(l).unwrap() + } else { + Server::bind(&([127,0,0,1], 3030).into()) + }; + + server.serve(make_svc).await.unwrap(); +} \ No newline at end of file From de6adb65f8e111ce7d75f32872b7027a85c1016b Mon Sep 17 00:00:00 2001 From: Adrian Kappel Date: Tue, 18 Feb 2020 13:24:23 -0500 Subject: [PATCH 2/4] Added EOF newline --- examples/autoreload.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/autoreload.rs b/examples/autoreload.rs index 80bec4f9c..7fc2d1726 100644 --- a/examples/autoreload.rs +++ b/examples/autoreload.rs @@ -33,4 +33,4 @@ async fn main() { }; server.serve(make_svc).await.unwrap(); -} \ No newline at end of file +} From ce305847e06a21d91d0094e8b9eef6e4278656c5 Mon Sep 17 00:00:00 2001 From: Adrian Kappel Date: Tue, 18 Feb 2020 13:33:58 -0500 Subject: [PATCH 3/4] Added better comments --- examples/autoreload.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/autoreload.rs b/examples/autoreload.rs index 7fc2d1726..1a209b418 100644 --- a/examples/autoreload.rs +++ b/examples/autoreload.rs @@ -18,14 +18,18 @@ async fn main() { // Match any request and return hello world! let routes = warp::any().map(|| "Hello, World!"); - // Convert warp filter into a hyper service, allowing us to later use - // it with `hyper::Server::from_tcp(...).serve(make_svc)`. + // hyper let's us build a server from a TcpListener (which will be + // useful shortly). Thus, we'll need to convert our `warp::Filter` into + // a `hyper::service::MakeService` for use with a `hyper::server::Server`. let svc = warp::service(routes); let make_svc = hyper::service::make_service_fn(|_: _| async move { Ok::<_, Infallible>(svc) }); let mut listenfd = ListenFd::from_env(); + // if listenfd doesn't take a TcpListener (i.e. we're not running via + // the command above), we fall back to explicitly binding to a given + // host:port. let server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() { Server::from_tcp(l).unwrap() } else { From 3bb923a2eb0e0801fa79ec5e867b0f5d4922b54c Mon Sep 17 00:00:00 2001 From: Adrian Kappel Date: Tue, 18 Feb 2020 13:46:12 -0500 Subject: [PATCH 4/4] Fix fmt check errors --- examples/autoreload.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/examples/autoreload.rs b/examples/autoreload.rs index 1a209b418..4aabfdb6a 100644 --- a/examples/autoreload.rs +++ b/examples/autoreload.rs @@ -1,8 +1,8 @@ #![deny(warnings)] -use std::convert::Infallible; -use warp::Filter; use hyper::server::Server; use listenfd::ListenFd; +use std::convert::Infallible; +use warp::Filter; extern crate listenfd; /// You'll need to install `systemfd` and `cargo-watch`: @@ -22,9 +22,7 @@ async fn main() { // useful shortly). Thus, we'll need to convert our `warp::Filter` into // a `hyper::service::MakeService` for use with a `hyper::server::Server`. let svc = warp::service(routes); - let make_svc = hyper::service::make_service_fn(|_: _| async move { - Ok::<_, Infallible>(svc) - }); + let make_svc = hyper::service::make_service_fn(|_: _| async move { Ok::<_, Infallible>(svc) }); let mut listenfd = ListenFd::from_env(); // if listenfd doesn't take a TcpListener (i.e. we're not running via @@ -33,7 +31,7 @@ async fn main() { let server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() { Server::from_tcp(l).unwrap() } else { - Server::bind(&([127,0,0,1], 3030).into()) + Server::bind(&([127, 0, 0, 1], 3030).into()) }; server.serve(make_svc).await.unwrap();