128 lines
4.0 KiB
Rust
128 lines
4.0 KiB
Rust
use axum::{extract::State, handler::Handler, response::IntoResponse};
|
|
use axum_extra::extract::cookie::{Cookie, CookieJar};
|
|
use clap::Parser;
|
|
use morethantext::MoreThanText;
|
|
use tokio::{spawn, sync::mpsc::channel};
|
|
|
|
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>,
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
let args = Args::parse();
|
|
let addr = format!("{}:{}", args.address, args.port);
|
|
let state = MoreThanText::new();
|
|
let app = mtt_conn.with_state(state);
|
|
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
|
|
axum::serve(listener, app.into_make_service())
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
async fn mtt_conn(jar: CookieJar, state: State<MoreThanText>) -> impl IntoResponse {
|
|
let sid = match jar.get(SESSION_KEY) {
|
|
Some(cookie) => Some(cookie.value().to_string()),
|
|
None => None,
|
|
};
|
|
let (tx, mut rx) = channel(5);
|
|
spawn(async move {
|
|
tx.send(state.clone().request(sid)).await.unwrap();
|
|
});
|
|
let reply = rx.recv().await.unwrap();
|
|
let cookie = Cookie::build((SESSION_KEY, reply.get_session().to_string()));
|
|
let cookies = jar.add(cookie);
|
|
|
|
/*
|
|
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, reply.get_content())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod servers {
|
|
use super::*;
|
|
use axum::{
|
|
body::Body,
|
|
http::{Request, StatusCode},
|
|
};
|
|
use tower::{Service, ServiceExt};
|
|
|
|
#[tokio::test]
|
|
async fn get_home_page() {
|
|
let app = mtt_conn.with_state(MoreThanText::new());
|
|
let response = app
|
|
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let sessid = format!("{:?}", response.headers().get("set-cookie").unwrap());
|
|
assert!(sessid.contains(SESSION_KEY), "did not set session id");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn session_ids_are_unique() {
|
|
let app = mtt_conn.with_state(MoreThanText::new());
|
|
let mut holder: Vec<String> = Vec::new();
|
|
for _ in 0..5 {
|
|
let response = app
|
|
.clone()
|
|
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
|
.await
|
|
.unwrap();
|
|
let sessid = format!("{:?}", response.headers().get("set-cookie").unwrap());
|
|
assert!(
|
|
!holder.contains(&sessid),
|
|
"found duplicate entry: {:?}",
|
|
holder
|
|
);
|
|
holder.push(sessid);
|
|
}
|
|
}
|
|
|
|
// Next test to complete.
|
|
//#[tokio::test]
|
|
async fn session_only_sent_once() {
|
|
let mut app = mtt_conn.with_state(MoreThanText::new());
|
|
let request = Request::builder().uri("/").body(Body::empty()).unwrap();
|
|
let response = ServiceExt::<Request<Body>>::ready(&mut app)
|
|
.await
|
|
.unwrap()
|
|
.call(request)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let request = Request::builder().uri("/").body(Body::empty()).unwrap();
|
|
let response = ServiceExt::<Request<Body>>::ready(&mut app)
|
|
.await
|
|
.unwrap()
|
|
.call(request)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let session = response.headers().get("set-cookie");
|
|
assert!(session.is_none(), "should only send the session once");
|
|
}
|
|
}
|