Preparing to move session into an extractor.

This commit is contained in:
2025-04-19 07:57:16 -04:00
parent 9e6d407b69
commit bb70cc65e0
5 changed files with 146 additions and 38 deletions

View File

@ -1,8 +1,19 @@
use axum::{extract::State, response::IntoResponse, routing::get, Router};
use axum::{
async_trait,
extract::{Extension, FromRequestParts, State},
http::request::Parts,
response::IntoResponse,
routing::get,
RequestPartsExt,
Router,
};
use axum_extra::extract::cookie::{Cookie, CookieJar};
use clap::Parser;
use morethantext::MoreThanText;
use std::convert::Infallible;
use tokio::{spawn, sync::mpsc::channel};
use tower_cookies::{CookieManagerLayer, Cookies};
use uuid::Uuid;
const LOCALHOST: &str = "127.0.0.1";
const SESSION_KEY: &str = "sessionid";
@ -33,11 +44,30 @@ async fn main() {
.unwrap();
}
struct SessionID;
#[async_trait]
impl<S> FromRequestParts<S> for SessionID
where
S: Send + Sync,
{
type Rejection = Infallible;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let cookies = parts.extract::<Extension<Cookies>>();
Ok(Self {})
}
}
async fn create_app(state: MoreThanText) -> Router {
Router::new().route("/", get(mtt_conn)).with_state(state)
}
async fn mtt_conn(jar: CookieJar, state: State<MoreThanText>) -> impl IntoResponse {
async fn mtt_conn(
jar: CookieJar,
sess_id: SessionID,
state: State<MoreThanText>,
) -> impl IntoResponse {
let sid = match jar.get(SESSION_KEY) {
Some(cookie) => Some(cookie.value().to_string()),
None => None,
@ -48,9 +78,9 @@ async fn mtt_conn(jar: CookieJar, state: State<MoreThanText>) -> impl IntoRespon
tx.send(state.clone().request(sess_info)).await.unwrap();
});
let reply = rx.recv().await.unwrap();
let cookie = Cookie::build((SESSION_KEY, reply.get_session().to_string()));
let cookie = Cookie::build((SESSION_KEY, reply.get_data("sess_id").unwrap().to_string()));
let cookies = jar.add(cookie);
(cookies, reply.get_content())
(cookies, reply.get_data("dov").unwrap().to_string())
}
#[cfg(test)]