use axum::{extract::State, response::IntoResponse, routing::get, Router}; use axum_extra::extract::cookie::{Cookie, CookieJar}; use clap::Parser; use morethantext::{MoreThanText, Session}; const LOCALHOST: &str = "127.0.0.1"; const SESSION_KEY: &str = "sessionid"; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Args { /// Post used #[arg(short, long, default_value_t = 3000)] port: u16, /// IP used #[arg(short, long, default_value_t = LOCALHOST.to_string())] address: String, /// cluster host #[arg(short, long, num_args(0..))] cluster: Option, } #[cfg(test)] mod http_session { #[tokio::test] async fn my_test() { assert!(true); } } #[tokio::main] async fn main() { let args = Args::parse(); let addr = format!("{}:{}", args.address, args.port); let state = MoreThanText::new(); let app = Router::new().route("/", get(handler)).with_state(state); let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); axum::serve(listener, app.into_make_service()) .await .unwrap(); } async fn handler(jar: CookieJar, state: State) -> impl IntoResponse { let cookies: CookieJar; let sid: Option; match jar.get(SESSION_KEY) { Some(cookie) => sid = Some(cookie.value().to_string()), None => sid = None, } match state.get_session(sid) { Session::Ok => cookies = jar, Session::New(id) => { let cookie = Cookie::build((SESSION_KEY, id.clone())).domain("example.com"); cookies = jar.add(cookie); } } (cookies, "Something goes here.") }