Change routing DSL

This commit is contained in:
David Pedersen
2021-06-04 01:00:48 +02:00
parent e156bc40e1
commit c3977d0b71
8 changed files with 736 additions and 1014 deletions
+4 -10
View File
@@ -2,20 +2,14 @@ use http::{Request, StatusCode};
use hyper::Server;
use std::net::SocketAddr;
use tower::make::Shared;
use tower_web::{body::Body, extract, response::Html};
use tower_web::{body::Body, response, get, route, AddRoute, extract};
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
// build our application with some routes
let app = tower_web::app()
.at("/")
.get(handler)
.at("/greet/:name")
.get(greet)
// convert it into a `Service`
.into_service();
let app = route("/", get(handler)).route("/greet/:name", get(greet));
// run it with hyper
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
@@ -24,8 +18,8 @@ async fn main() {
server.await.unwrap();
}
async fn handler(_req: Request<Body>) -> Html<&'static str> {
Html("<h1>Hello, World!</h1>")
async fn handler(_req: Request<Body>) -> response::Html<&'static str> {
response::Html("<h1>Hello, World!</h1>")
}
async fn greet(_req: Request<Body>, params: extract::UrlParamsMap) -> Result<String, StatusCode> {