2024-03-11 10:45:20 -04:00
|
|
|
use axum::{
|
|
|
|
response::IntoResponse,
|
|
|
|
routing::get,
|
|
|
|
Router,
|
|
|
|
};
|
|
|
|
use axum_extra::extract::cookie::{CookieJar, Cookie};
|
2024-02-29 11:20:20 -05:00
|
|
|
use clap::Parser;
|
|
|
|
|
2024-02-29 18:46:01 -05:00
|
|
|
const LOCALHOST: &str = "127.0.0.1";
|
2024-03-11 10:45:20 -04:00
|
|
|
const SESSION_KEY: &str = "sessionid";
|
2024-02-29 18:46:01 -05:00
|
|
|
|
2024-02-29 11:20:20 -05:00
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[command(version, about, long_about = None)]
|
|
|
|
struct Args {
|
|
|
|
/// Post used
|
|
|
|
#[arg(short, long, default_value_t = 3000)]
|
2024-02-29 18:46:01 -05:00
|
|
|
port: u16,
|
|
|
|
/// IP used
|
|
|
|
#[arg(short, long, default_value_t = LOCALHOST.to_string())]
|
|
|
|
address: String,
|
2024-02-29 11:20:20 -05:00
|
|
|
}
|
|
|
|
|
2024-03-11 10:45:20 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod http_session {
|
|
|
|
#[tokio::test]
|
|
|
|
async fn my_test() {
|
|
|
|
assert!(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 11:20:20 -05:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
let args = Args::parse();
|
2024-03-11 10:45:20 -04:00
|
|
|
let addr = format!("{}:{}", args.address, args.port);
|
|
|
|
let app = Router::new()
|
|
|
|
.route("/", get(handler));
|
|
|
|
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
|
|
|
|
axum::serve(listener, app.into_make_service())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-02-29 11:20:20 -05:00
|
|
|
}
|
|
|
|
|
2024-03-11 10:45:20 -04:00
|
|
|
async fn handler(jar: CookieJar) -> impl IntoResponse {
|
|
|
|
let cookies: CookieJar;
|
|
|
|
let id: String;
|
|
|
|
match jar.get(SESSION_KEY) {
|
|
|
|
Some(session) => {
|
|
|
|
id = session.to_string();
|
|
|
|
cookies = jar;
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
id = "Fred".to_string();
|
|
|
|
cookies = jar.add(Cookie::new(SESSION_KEY, id.clone()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(cookies, format!("id is {}", id))
|
2024-02-26 08:41:24 -05:00
|
|
|
}
|