libslonk/examples/web.rs
2025-02-08 23:03:58 +03:00

26 lines
733 B
Rust

use axum::middleware::from_fn;
use axum::routing::get;
use axum::{Extension, Router};
use libslonk::filtered_path::{FilteredPath, SlashFilter};
use libslonk::{request_id, trace_layer_with_ulid};
use tokio::net::TcpListener;
use ulid::Ulid;
async fn say_hello(
Extension(id): Extension<Ulid>,
FilteredPath(name, ..): FilteredPath<String, SlashFilter>,
) -> String {
format!("Hello, {name}. Your request has the ULID: {id}\n")
}
#[tokio::main]
async fn main() {
let router = Router::new()
.route("/{name}", get(say_hello))
.layer(trace_layer_with_ulid!())
.layer(from_fn(request_id));
axum::serve(TcpListener::bind("[::]:3000").await.unwrap(), router)
.await
.unwrap();
}