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
35
36
37
38
39
40
41
42
use httpz::{
http::{Method, Response, StatusCode},
GenericEndpoint, Request,
};
#[cfg(all(feature = "axum", feature = "cookies"))]
#[tokio::main]
async fn main() {
use cookie::Cookie;
let endpoint = GenericEndpoint::new(
"/",
[Method::GET, Method::POST],
|req: Request| async move {
// DO THIS
let mut cookies = req.cookies(); // This creates a new CookieJar from the request.
cookies.add(Cookie::new("foo", "bar"));
// DON'T DO THIS
// It will create a new CookieJar, set a cookie on it and then drop it. The cookies won't be set on the response because it's not returned from handler.
// req.cookies().add(...);
Ok((
Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "text/html")
.body(b"Hello httpz World!".to_vec())?,
cookies, // If you don't return the CookieJar the cookies won't be set.
))
},
);
// Attach your endpoint to a HTTP server. This example uses Axum but it could be any other one.
let app = axum::Router::new().nest("/", endpoint.axum());
let addr = "[::]:9000".parse::<std::net::SocketAddr>().unwrap(); // This listens on IPv6 and IPv4
println!("Axum listening on http://{}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}