Tweak hello world example

This commit is contained in:
David Pedersen 2021-06-06 15:26:33 +02:00
parent 35ab973acb
commit b4e2750d6a
2 changed files with 6 additions and 10 deletions

View file

@ -34,17 +34,15 @@ use tower::make::Shared;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// build our application with a single route // build our application with a single route
let app = route("/", get(handler)); let app = route("/", get(|request: Request<Body>| async {
"Hello, World!"
}));
// run it with hyper on localhost:3000 // run it with hyper on localhost:3000
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let server = Server::bind(&addr).serve(Shared::new(app)); let server = Server::bind(&addr).serve(Shared::new(app));
server.await.unwrap(); server.await.unwrap();
} }
async fn handler(req: Request<Body>) -> &'static str {
"Hello, World!"
}
``` ```
## Routing ## Routing

View file

@ -32,17 +32,15 @@
//! #[tokio::main] //! #[tokio::main]
//! async fn main() { //! async fn main() {
//! // build our application with a single route //! // build our application with a single route
//! let app = route("/", get(handler)); //! let app = route("/", get(|request: Request<Body>| async {
//! "Hello, World!"
//! }));
//! //!
//! // run it with hyper on localhost:3000 //! // run it with hyper on localhost:3000
//! let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); //! let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
//! let server = Server::bind(&addr).serve(Shared::new(app)); //! let server = Server::bind(&addr).serve(Shared::new(app));
//! server.await.unwrap(); //! server.await.unwrap();
//! } //! }
//!
//! async fn handler(req: Request<Body>) -> &'static str {
//! "Hello, World!"
//! }
//! ``` //! ```
//! //!
//! # Routing //! # Routing