2024-03-17 15:40:00 -04:00
|
|
|
use axum::{response::IntoResponse, routing::get, Router};
|
|
|
|
use axum_extra::extract::cookie::{Cookie, CookieJar};
|
2024-02-29 11:20:20 -05:00
|
|
|
use clap::Parser;
|
2024-03-17 15:40:00 -04:00
|
|
|
use rand::distributions::{Alphanumeric, DistString};
|
2024-02-29 11:20:20 -05:00
|
|
|
|
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);
|
2024-03-17 15:40:00 -04:00
|
|
|
let app = Router::new().route("/", get(handler));
|
2024-03-11 10:45:20 -04:00
|
|
|
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;
|
2024-03-17 15:40:00 -04:00
|
|
|
}
|
2024-03-11 10:45:20 -04:00
|
|
|
None => {
|
2024-03-17 15:40:00 -04:00
|
|
|
id = Alphanumeric.sample_string(&mut rand::thread_rng(), 16);
|
|
|
|
let cookie = Cookie::build((SESSION_KEY, id.clone())).domain("example.com");
|
|
|
|
cookies = jar.add(cookie);
|
2024-03-11 10:45:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
(cookies, format!("id is {}", id))
|
2024-02-26 08:41:24 -05:00
|
|
|
}
|