libslonk/examples/web.rs

27 lines
733 B
Rust
Raw Permalink Normal View History

2025-02-06 17:08:51 +03:00
use axum::middleware::from_fn;
use axum::routing::get;
use axum::{Extension, Router};
2025-02-08 22:59:13 +03:00
use libslonk::filtered_path::{FilteredPath, SlashFilter};
2025-02-06 17:08:51 +03:00
use libslonk::{request_id, trace_layer_with_ulid};
use tokio::net::TcpListener;
use ulid::Ulid;
2025-02-08 22:59:13 +03:00
async fn say_hello(
Extension(id): Extension<Ulid>,
FilteredPath(name, ..): FilteredPath<String, SlashFilter>,
) -> String {
2025-02-06 17:08:51 +03:00
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();
}