diff --git a/README.md b/README.md index 043de727f..124409d03 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ Ecosystem WG, and **not ready for production use yet**. fn main() -> Result<(), std::io::Error> { let mut app = tide::App::new(); app.at("/").get(async move |_| "Hello, world!"); - Ok(app.serve("127.0.0.1:8000")?) + Ok(app.run("127.0.0.1:8000")?) } ``` diff --git a/examples/src/body_types.rs b/examples/src/body_types.rs index 0cc0e66a1..9df1509a5 100644 --- a/examples/src/body_types.rs +++ b/examples/src/body_types.rs @@ -47,5 +47,5 @@ pub fn main() { app.at("/echo/json").post(echo_json); app.at("/echo/form").post(echo_form); - app.serve("127.0.0.1:8000").unwrap(); + app.run("127.0.0.1:8000").unwrap(); } diff --git a/examples/src/catch_all.rs b/examples/src/catch_all.rs index 69947f1f7..ebdf736c3 100644 --- a/examples/src/catch_all.rs +++ b/examples/src/catch_all.rs @@ -8,5 +8,5 @@ async fn echo_path(cx: Context<()>) -> String { pub fn main() { let mut app = tide::App::new(); app.at("/echo_path/*path").get(echo_path); - app.serve("127.0.0.1:8000").unwrap(); + app.run("127.0.0.1:8000").unwrap(); } diff --git a/examples/src/cookies.rs b/examples/src/cookies.rs index 4ebff0738..a43dc67c1 100644 --- a/examples/src/cookies.rs +++ b/examples/src/cookies.rs @@ -23,5 +23,5 @@ pub fn main() { app.at("/").get(retrieve_cookie); app.at("/set").get(set_cookie); app.at("/remove").get(remove_cookie); - app.serve("127.0.0.1:8000").unwrap(); + app.run("127.0.0.1:8000").unwrap(); } diff --git a/examples/src/default_headers.rs b/examples/src/default_headers.rs index 23f057e30..b76bfddc7 100644 --- a/examples/src/default_headers.rs +++ b/examples/src/default_headers.rs @@ -11,5 +11,5 @@ pub fn main() { app.at("/").get(async move |_| "Hello, world!"); - app.serve("127.0.0.1:8000").unwrap(); + app.run("127.0.0.1:8000").unwrap(); } diff --git a/examples/src/graphql.rs b/examples/src/graphql.rs index 7a263cbcf..a85af2e69 100644 --- a/examples/src/graphql.rs +++ b/examples/src/graphql.rs @@ -59,5 +59,5 @@ async fn handle_graphql(mut cx: Context) -> EndpointResult { pub fn main() { let mut app = App::with_state(Data::default()); app.at("/graphql").post(handle_graphql); - app.serve("127.0.0.1:8000").unwrap(); + app.run("127.0.0.1:8000").unwrap(); } diff --git a/examples/src/hello.rs b/examples/src/hello.rs index 83e246463..1a11e05c9 100644 --- a/examples/src/hello.rs +++ b/examples/src/hello.rs @@ -1,5 +1,5 @@ pub fn main() { let mut app = tide::App::new(); app.at("/").get(async move |_| "Hello, world!"); - app.serve("127.0.0.1:8000").unwrap(); + app.run("127.0.0.1:8000").unwrap(); } diff --git a/examples/src/messages.rs b/examples/src/messages.rs index 3ec8db93e..4cf905f79 100644 --- a/examples/src/messages.rs +++ b/examples/src/messages.rs @@ -68,5 +68,5 @@ pub fn main() { let mut app = App::with_state(Database::default()); app.at("/message").post(new_message); app.at("/message/:id").get(get_message).post(set_message); - app.serve("127.0.0.1:8000").unwrap(); + app.run("127.0.0.1:8000").unwrap(); } diff --git a/examples/src/multipart_form/mod.rs b/examples/src/multipart_form/mod.rs index fd3485b12..ce4d74ca3 100644 --- a/examples/src/multipart_form/mod.rs +++ b/examples/src/multipart_form/mod.rs @@ -58,7 +58,7 @@ async fn upload_file(mut cx: Context<()>) -> EndpointResult { pub fn run() { let mut app = App::new(); app.at("/upload_file").post(upload_file); - app.serve("127.0.0.1:8000").unwrap(); + app.run("127.0.0.1:8000").unwrap(); } // Test with: diff --git a/examples/src/staticfile.rs b/examples/src/staticfile.rs index 63524b36b..cf44ccd97 100644 --- a/examples/src/staticfile.rs +++ b/examples/src/staticfile.rs @@ -122,5 +122,5 @@ async fn handle_path(ctx: Context) -> EndpointResult { pub fn main() { let mut app = App::with_state(StaticFile::new("./")); app.at("/*").get(handle_path); - app.serve("127.0.0.1:8000").unwrap(); + app.run("127.0.0.1:8000").unwrap(); } diff --git a/tide/src/app.rs b/tide/src/app.rs index 7e4273036..83cd3f517 100644 --- a/tide/src/app.rs +++ b/tide/src/app.rs @@ -34,7 +34,7 @@ use crate::{ /// /// let mut app = tide::App::new(); /// app.at("/hello").get(async move |_| "Hello, world!"); -/// app.serve("127.0.0.1:8000"); +/// app.run("127.0.0.1:8000"); /// ``` /// /// # Routing and parameters @@ -67,7 +67,7 @@ use crate::{ /// "Use /hello/{your name} or /goodbye/{your name}" /// }); /// -/// app.serve("127.0.0.1:8000"); +/// app.run("127.0.0.1:8000"); /// ``` /// /// You can learn more about routing in the [`App::at`] documentation. @@ -123,7 +123,7 @@ use crate::{ /// let mut app = App::with_state(Database::default()); /// app.at("/message").post(new_message); /// app.at("/message/:id").get(get_message); -/// app.serve("127.0.0.1:8000").unwrap(); +/// app.run("127.0.0.1:8000").unwrap(); /// } /// ``` @@ -232,11 +232,11 @@ impl App { } } - /// Start serving the app at the given address. + /// Run the app at the given address. /// /// Blocks the calling thread indefinitely. #[cfg(feature = "hyper")] - pub fn serve(self, addr: impl std::net::ToSocketAddrs) -> std::io::Result<()> { + pub fn run(self, addr: impl std::net::ToSocketAddrs) -> std::io::Result<()> { let addr = addr .to_socket_addrs()? .next() @@ -246,6 +246,19 @@ impl App { http_service_hyper::run(self.into_http_service(), addr); Ok(()) } + + /// Asynchronously serve the app at the given address. + #[cfg(feature = "hyper")] + pub async fn serve(self, addr: impl std::net::ToSocketAddrs) -> std::io::Result<()> { + let addr = addr + .to_socket_addrs()? + .next() + .ok_or(std::io::ErrorKind::InvalidInput)?; + + // TODO: propagate the error from hyper + http_service_hyper::serve(self.into_http_service(), addr).await.ok(); + Ok(()) + } } /// An instantiated Tide server. diff --git a/tide/src/endpoint.rs b/tide/src/endpoint.rs index f4adbfcf8..8ec5e988b 100644 --- a/tide/src/endpoint.rs +++ b/tide/src/endpoint.rs @@ -28,7 +28,7 @@ use crate::{response::IntoResponse, Context, Response}; /// fn main() { /// let mut app = tide::App::new(); /// app.at("/hello").get(hello); -/// app.serve("127.0.0.1:8000").unwrap() +/// app.run("127.0.0.1:8000").unwrap() /// } /// ``` /// @@ -43,7 +43,7 @@ use crate::{response::IntoResponse, Context, Response}; /// fn main() { /// let mut app = tide::App::new(); /// app.at("/hello").get(hello); -/// app.serve("127.0.0.1:8000").unwrap() +/// app.run("127.0.0.1:8000").unwrap() /// } /// ``` ///