-
-
Notifications
You must be signed in to change notification settings - Fork 169
/
main.rs
34 lines (29 loc) · 1.04 KB
/
main.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
33
34
use std::fmt::Write;
use salvo::prelude::*;
use salvo_flash::{CookieStore, FlashDepotExt};
#[handler]
pub async fn set_flash(depot: &mut Depot, res: &mut Response) {
let flash = depot.outgoing_flash_mut();
flash.info("Hey there!").debug("How is it going?");
res.render(Redirect::other("/get").unwrap());
}
#[handler]
pub async fn get_flash(depot: &mut Depot, _res: &mut Response) -> String {
let mut body = String::new();
if let Some(flash) = depot.incoming_flash() {
for message in flash.iter() {
writeln!(body, "{} - {}", message.value, message.level).unwrap();
}
}
body
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt().init();
tracing::info!("Listening on http://127.0.0.1:7878");
let router = Router::new()
.hoop(CookieStore::new().into_handler())
.push(Router::with_path("get").get(get_flash))
.push(Router::with_path("set").get(set_flash));
Server::new(TcpListener::bind("127.0.0.1:7878")).serve(router).await;
}