got session id working.
This commit is contained in:
19
src/main.rs
19
src/main.rs
@ -1,10 +1,7 @@
|
||||
use axum::{
|
||||
response::IntoResponse,
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use axum_extra::extract::cookie::{CookieJar, Cookie};
|
||||
use axum::{response::IntoResponse, routing::get, Router};
|
||||
use axum_extra::extract::cookie::{Cookie, CookieJar};
|
||||
use clap::Parser;
|
||||
use rand::distributions::{Alphanumeric, DistString};
|
||||
|
||||
const LOCALHOST: &str = "127.0.0.1";
|
||||
const SESSION_KEY: &str = "sessionid";
|
||||
@ -32,8 +29,7 @@ mod http_session {
|
||||
async fn main() {
|
||||
let args = Args::parse();
|
||||
let addr = format!("{}:{}", args.address, args.port);
|
||||
let app = Router::new()
|
||||
.route("/", get(handler));
|
||||
let app = Router::new().route("/", get(handler));
|
||||
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
|
||||
axum::serve(listener, app.into_make_service())
|
||||
.await
|
||||
@ -47,10 +43,11 @@ async fn handler(jar: CookieJar) -> impl IntoResponse {
|
||||
Some(session) => {
|
||||
id = session.to_string();
|
||||
cookies = jar;
|
||||
},
|
||||
}
|
||||
None => {
|
||||
id = "Fred".to_string();
|
||||
cookies = jar.add(Cookie::new(SESSION_KEY, id.clone()));
|
||||
id = Alphanumeric.sample_string(&mut rand::thread_rng(), 16);
|
||||
let cookie = Cookie::build((SESSION_KEY, id.clone())).domain("example.com");
|
||||
cookies = jar.add(cookie);
|
||||
}
|
||||
}
|
||||
(cookies, format!("id is {}", id))
|
||||
|
Reference in New Issue
Block a user