Abandoned path. Senders can only be moved oncde.
This commit is contained in:
77
src/main.rs
77
src/main.rs
@ -1,8 +1,8 @@
|
||||
use axum::{extract::State, response::IntoResponse, routing::get, Router};
|
||||
use axum::{extract::State, handler::Handler, response::IntoResponse};
|
||||
use axum_extra::extract::cookie::{Cookie, CookieJar};
|
||||
use clap::Parser;
|
||||
//use morethantext::{MoreThanText, Session};
|
||||
use morethantext::MoreThanText;
|
||||
use tokio::{spawn, sync::mpsc::channel};
|
||||
|
||||
const LOCALHOST: &str = "127.0.0.1";
|
||||
const SESSION_KEY: &str = "sessionid";
|
||||
@ -21,28 +21,32 @@ struct Args {
|
||||
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 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 handler(jar: CookieJar, mut state: State<MoreThanText>) -> impl IntoResponse {
|
||||
let mut cookies = jar.clone();
|
||||
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.request(sid)).await.unwrap();
|
||||
});
|
||||
let reply = rx.recv().await.unwrap();
|
||||
let cookie = Cookie::build((SESSION_KEY, reply.get_session()));
|
||||
let cookies = jar.add(cookie);
|
||||
|
||||
/*
|
||||
let sid = match jar.get(SESSION_KEY) {
|
||||
Some(cookie) => Some(cookie.value().to_string()),
|
||||
None => None,
|
||||
@ -52,5 +56,50 @@ async fn handler(jar: CookieJar, mut state: State<MoreThanText>) -> impl IntoRes
|
||||
let cookie = Cookie::build((SESSION_KEY, state.get_id()));
|
||||
cookies = jar.add(cookie);
|
||||
}
|
||||
(cookies, "Something goes here.")
|
||||
*/
|
||||
(cookies, reply.get_content())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod servers {
|
||||
use super::*;
|
||||
use axum::{
|
||||
body::Body,
|
||||
http::{Request, StatusCode},
|
||||
};
|
||||
use tower::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);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
Reference in New Issue
Block a user