Files
axum/examples/hello-world/src/main.rs
T

46 lines
936 B
Rust
Raw Normal View History

//! Run with
//!
//! ```not_rust
//! cargo run -p example-hello-world
//! ```
2022-02-10 16:46:42 +01:00
#![allow(dead_code)]
2021-11-02 13:07:34 +01:00
use axum::{response::Html, routing::get, Router};
2022-02-11 10:40:25 +01:00
use axum_macros::Uri;
2022-02-10 16:46:42 +01:00
use serde::Deserialize;
2021-11-02 13:07:34 +01:00
use std::net::SocketAddr;
#[tokio::main]
async fn main() {
2021-11-02 13:07:34 +01:00
// build our application with a route
let app = Router::new().route("/", get(handler));
2021-11-02 13:07:34 +01:00
// run it
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
2021-11-02 13:07:34 +01:00
2022-02-10 16:46:42 +01:00
async fn handler(_: UsersShow) -> Html<&'static str> {
2021-11-02 13:07:34 +01:00
Html("<h1>Hello, World!</h1>")
}
2022-02-10 16:46:42 +01:00
2022-02-11 10:40:25 +01:00
#[derive(Deserialize, Uri)]
#[uri("/users")]
struct UsersIndex;
2022-02-10 16:46:42 +01:00
2022-02-11 10:40:25 +01:00
#[derive(Deserialize, Uri)]
#[uri("/users/:id/teams/:team_id")]
2022-02-10 16:46:42 +01:00
struct UsersShow {
id: u32,
team_id: u32,
}
// #[derive(serde::Deserialize)]
// #[route("/users/:id/edit")]
// struct UsersEdit(u32);