-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcounter.rs
32 lines (28 loc) · 953 Bytes
/
counter.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use axum::{routing::get, Router};
use std::net::SocketAddr;
use tower_cookies::{Cookie, CookieManagerLayer, Cookies};
const COOKIE_NAME: &str = "visited";
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(handler))
.layer(CookieManagerLayer::new());
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
}
async fn handler(cookies: Cookies) -> String {
let visited = cookies
.get(COOKIE_NAME)
.and_then(|c| c.value().parse().ok())
.unwrap_or(0);
if visited > 10 {
cookies.remove(Cookie::new(COOKIE_NAME, ""));
"Counter has been reset".into()
} else {
cookies.add(Cookie::new(COOKIE_NAME, (visited + 1).to_string()));
format!("You've been here {} times before", visited)
}
}