morethantext/src/main.rs

57 lines
1.6 KiB
Rust
Raw Normal View History

2024-03-19 19:54:14 -04:00
use axum::{extract::State, response::IntoResponse, routing::get, Router};
2024-03-17 15:40:00 -04:00
use axum_extra::extract::cookie::{Cookie, CookieJar};
use clap::Parser;
2024-05-12 14:10:36 -04:00
//use morethantext::{MoreThanText, Session};
use morethantext::MoreThanText;
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
#[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-03-29 07:51:14 -04:00
/// cluster host
#[arg(short, long, num_args(0..))]
2024-07-30 15:11:31 -04:00
node: Vec<String>,
}
2024-03-11 10:45:20 -04:00
#[cfg(test)]
mod http_session {
#[tokio::test]
async fn my_test() {
assert!(true);
}
}
#[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-11-06 21:05:52 -05:00
let state = MoreThanText::new();
2024-03-19 19:54:14 -04:00
let app = Router::new().route("/", get(handler)).with_state(state);
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-05-12 14:10:36 -04:00
async fn handler(jar: CookieJar, mut state: State<MoreThanText>) -> impl IntoResponse {
let mut cookies = jar.clone();
2024-11-06 21:05:52 -05:00
let sid = match jar.get(SESSION_KEY) {
Some(cookie) => Some(cookie.value().to_string()),
None => None,
};
2024-05-12 14:10:36 -04:00
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);
2024-03-11 10:45:20 -04:00
}
2024-03-19 19:54:14 -04:00
(cookies, "Something goes here.")
2024-02-26 08:41:24 -05:00
}