-
Notifications
You must be signed in to change notification settings - Fork 321
/
cookies.rs
34 lines (28 loc) · 990 Bytes
/
cookies.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 tide::http::Cookie;
use tide::{Request, Response, StatusCode};
/// Tide will use the the `Cookies`'s `Extract` implementation to build this parameter.
///
async fn retrieve_cookie(req: Request<()>) -> tide::Result<String> {
Ok(format!("hello cookies: {:?}", req.cookie("hello").unwrap()))
}
async fn insert_cookie(_req: Request<()>) -> tide::Result {
let mut res = Response::new(StatusCode::Ok);
res.insert_cookie(Cookie::new("hello", "world"));
Ok(res)
}
async fn remove_cookie(_req: Request<()>) -> tide::Result {
let mut res = Response::new(StatusCode::Ok);
res.remove_cookie(Cookie::named("hello"));
Ok(res)
}
#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
femme::start();
let mut app = tide::new();
app.with(tide::log::LogMiddleware::new());
app.at("/").get(retrieve_cookie);
app.at("/set").get(insert_cookie);
app.at("/remove").get(remove_cookie);
app.listen("127.0.0.1:8080").await?;
Ok(())
}