mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-22 15:17:18 +01:00
32 lines
918 B
Rust
32 lines
918 B
Rust
use http::StatusCode;
|
|
use hyper::Server;
|
|
use std::net::SocketAddr;
|
|
use tower::make::Shared;
|
|
use tower_web::prelude::*;
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
tracing_subscriber::fmt::init();
|
|
|
|
// build our application with some routes
|
|
let app = route("/", get(handler)).route("/greet/:name", get(greet));
|
|
|
|
// 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();
|
|
}
|
|
|
|
async fn handler() -> response::Html<&'static str> {
|
|
response::Html("<h1>Hello, World!</h1>")
|
|
}
|
|
|
|
async fn greet(params: extract::UrlParamsMap) -> Result<String, StatusCode> {
|
|
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)
|
|
}
|
|
}
|