57 lines
1.6 KiB
Rust
57 lines
1.6 KiB
Rust
use axum::{extract::State, response::IntoResponse, routing::get, Router};
|
|
use axum_extra::extract::cookie::{Cookie, CookieJar};
|
|
use clap::Parser;
|
|
//use morethantext::{MoreThanText, Session};
|
|
use morethantext::MoreThanText;
|
|
|
|
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..))]
|
|
node: Vec<String>,
|
|
}
|
|
|
|
#[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, mut state: State<MoreThanText>) -> impl IntoResponse {
|
|
let mut cookies = jar.clone();
|
|
let sid = match jar.get(SESSION_KEY) {
|
|
Some(cookie) => Some(cookie.value().to_string()),
|
|
None => None,
|
|
};
|
|
state.open_session(sid.clone());
|
|
if !sid.is_some_and(|x| x == state.get_id()) {
|
|
let cookie = Cookie::build((SESSION_KEY, state.get_id()));
|
|
cookies = jar.add(cookie);
|
|
}
|
|
(cookies, "Something goes here.")
|
|
}
|