2021-06-09 09:03:09 +02:00
|
|
|
use http::StatusCode;
|
2021-05-31 10:20:07 +02:00
|
|
|
use hyper::Server;
|
|
|
|
use std::net::SocketAddr;
|
2021-06-01 11:23:56 +02:00
|
|
|
use tower::make::Shared;
|
2021-06-06 11:42:44 +02:00
|
|
|
use tower_web::prelude::*;
|
2021-05-31 10:20:07 +02:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
tracing_subscriber::fmt::init();
|
|
|
|
|
|
|
|
// build our application with some routes
|
2021-06-04 01:00:48 +02:00
|
|
|
let app = route("/", get(handler)).route("/greet/:name", get(greet));
|
2021-05-31 10:20:07 +02:00
|
|
|
|
|
|
|
// run it with hyper
|
|
|
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
|
|
|
tracing::debug!("listening on {}", addr);
|
|
|
|
let server = Server::bind(&addr).serve(Shared::new(app));
|
|
|
|
server.await.unwrap();
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:03:09 +02:00
|
|
|
async fn handler() -> response::Html<&'static str> {
|
2021-06-04 01:00:48 +02:00
|
|
|
response::Html("<h1>Hello, World!</h1>")
|
2021-05-31 10:20:07 +02:00
|
|
|
}
|
2021-06-01 11:23:56 +02:00
|
|
|
|
2021-06-09 09:03:09 +02:00
|
|
|
async fn greet(params: extract::UrlParamsMap) -> Result<String, StatusCode> {
|
2021-06-01 11:23:56 +02:00
|
|
|
if let Some(name) = params.get("name") {
|
|
|
|
Ok(format!("Hello {}!", name))
|
|
|
|
} else {
|
|
|
|
// if the route matches "name" will be present
|
|
|
|
Err(StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
}
|
|
|
|
}
|