Here's an example of an axum app keeping track of your visits to the page (full example is in examples/counter.rs):
rust
let app = Router::new()
.route(
"/",
get(|mut cookies: Cookies| async move {
let visited = if let Some(cookie) = cookies.get("visited") {
cookie.value().parse().ok().unwrap_or(0)
} else {
0
};
cookies.add(Cookie::new("visited", (visited + 1).to_string()));
format!("You've been here {} times before", visited)
}),
)
.layer(CookieLayer);