From d56a20ce81189f3fc8e4a954e9fed8674fb47fa8 Mon Sep 17 00:00:00 2001 From: Erik Hollensbe Date: Wed, 27 Apr 2022 07:42:39 -0700 Subject: [PATCH] fixed up the example Signed-off-by: Erik Hollensbe --- examples/hello-world-unix.rs | 30 ------------------------------ examples/hello-world.rs | 14 +++++++++++++- 2 files changed, 13 insertions(+), 31 deletions(-) delete mode 100644 examples/hello-world-unix.rs diff --git a/examples/hello-world-unix.rs b/examples/hello-world-unix.rs deleted file mode 100644 index 2ef0497..0000000 --- a/examples/hello-world-unix.rs +++ /dev/null @@ -1,30 +0,0 @@ -use std::path::PathBuf; - -use ratpack::prelude::*; - -async fn hello( - req: Request, - _resp: Option>, - params: Params, - _app: App<(), NoState>, - _state: NoState, -) -> HTTPResult { - let name = params.get("name").unwrap(); - let bytes = Body::from(format!("hello, {}!\n", name)); - - return Ok(( - req, - Some(Response::builder().status(200).body(bytes).unwrap()), - NoState {}, - )); -} - -#[tokio::main] -async fn main() -> Result<(), ServerError> { - let mut app = App::new(); - app.get("/:name", compose_handler!(hello)); - - app.serve_unix(PathBuf::from("/tmp/server.sock")).await?; - - Ok(()) -} diff --git a/examples/hello-world.rs b/examples/hello-world.rs index 79fedab..8036d17 100644 --- a/examples/hello-world.rs +++ b/examples/hello-world.rs @@ -1,4 +1,6 @@ use ratpack::prelude::*; +#[cfg(feature = "unix")] +use std::path::PathBuf; async fn hello( req: Request, @@ -22,7 +24,17 @@ async fn main() -> Result<(), ServerError> { let mut app = App::new(); app.get("/:name", compose_handler!(hello)); - app.serve("127.0.0.1:3000").await?; + #[cfg(feature = "unix")] + { + std::fs::remove_file("/tmp/server.sock").unwrap_or_default(); + eprintln!("Serving over /tmp/server.sock"); + app.serve_unix(PathBuf::from("/tmp/server.sock")).await?; + } + #[cfg(not(feature = "unix"))] + { + eprintln!("Serving over 127.0.0.1:3000"); + app.serve("127.0.0.1:3000").await?; + } Ok(()) }