Gave control to documents service.

This commit is contained in:
Jeff Baskin 2025-04-26 10:29:58 -04:00
parent 55ffa538e8
commit 1bfbfc5e44
4 changed files with 65 additions and 37 deletions

View File

@ -1,4 +1,7 @@
use crate::{ErrorType, queue::{Message, MsgType, Queue}}; use crate::{
queue::{Message, MsgType, Queue},
ErrorType,
};
use std::{ use std::{
sync::mpsc::{channel, Receiver}, sync::mpsc::{channel, Receiver},
thread::spawn, thread::spawn,
@ -40,7 +43,7 @@ impl Document {
output.add_data("error_type", ErrorType::DocumentNotFound); output.add_data("error_type", ErrorType::DocumentNotFound);
output output
} }
}, }
None => msg.reply(MsgType::Document), None => msg.reply(MsgType::Document),
}; };
reply.add_data("doc", "Something goes hwew"); reply.add_data("doc", "Something goes hwew");
@ -91,12 +94,12 @@ pub mod documents {
let reply = rx.recv_timeout(TIMEOUT).unwrap(); let reply = rx.recv_timeout(TIMEOUT).unwrap();
assert_eq!(reply.get_id(), msg.get_id()); assert_eq!(reply.get_id(), msg.get_id());
match reply.get_msg_type() { match reply.get_msg_type() {
MsgType::Error => {}, MsgType::Error => {}
_ => unreachable!("got {:?}: shoud have been error", reply.get_msg_type()), _ => unreachable!("got {:?}: shoud have been error", reply.get_msg_type()),
} }
match reply.get_data("error_type") { match reply.get_data("error_type") {
Some(err) => match err.to_error_type().unwrap() { Some(err) => match err.to_error_type().unwrap() {
ErrorType::DocumentNotFound => {}, ErrorType::DocumentNotFound => {}
_ => unreachable!("got {:?}: should have been document not found'", err), _ => unreachable!("got {:?}: should have been document not found'", err),
}, },
None => unreachable!("should contain error type"), None => unreachable!("should contain error type"),
@ -112,8 +115,11 @@ pub mod documents {
queue.send(msg); queue.send(msg);
let reply = rx.recv().unwrap(); let reply = rx.recv().unwrap();
match reply.get_msg_type() { match reply.get_msg_type() {
MsgType::Document => {}, MsgType::Document => {}
_ => unreachable!("Got '{:?}': should have been a document", reply.get_msg_type()), _ => unreachable!(
"Got '{:?}': should have been a document",
reply.get_msg_type()
),
} }
} }
} }

View File

@ -1,5 +1,5 @@
use chrono::prelude::*;
use crate::ErrorType; use crate::ErrorType;
use chrono::prelude::*;
use std::fmt; use std::fmt;
use uuid::Uuid; use uuid::Uuid;
@ -219,7 +219,7 @@ mod fields {
let field: Field = err.into(); let field: Field = err.into();
match field { match field {
Field::ErrorType(data) => match data { Field::ErrorType(data) => match data {
ErrorType::DocumentNotFound => {}, ErrorType::DocumentNotFound => {}
//_ => unreachable!("got {:?}: should have been Document not found", data), //_ => unreachable!("got {:?}: should have been Document not found", data),
}, },
_ => unreachable!("should have been an error type"), _ => unreachable!("should have been an error type"),
@ -238,7 +238,7 @@ mod fields {
let field: Field = err.into(); let field: Field = err.into();
let result = field.to_error_type().unwrap(); let result = field.to_error_type().unwrap();
match result { match result {
ErrorType::DocumentNotFound => {}, ErrorType::DocumentNotFound => {}
//_ => unreachable!("got {:?}: should have been document not found", result), //_ => unreachable!("got {:?}: should have been document not found", result),
} }
} }

View File

@ -73,7 +73,7 @@ mod mtt_replies {
let reply = MTTReply::new(msg); let reply = MTTReply::new(msg);
match reply.get_error() { match reply.get_error() {
Some(err) => match err { Some(err) => match err {
ErrorType::DocumentNotFound => {}, ErrorType::DocumentNotFound => {}
}, },
None => unreachable!("should return an error type"), None => unreachable!("should return an error type"),
} }
@ -94,7 +94,7 @@ mod mtt_replies {
let reply = MTTReply::new(msg); let reply = MTTReply::new(msg);
match reply.get_error() { match reply.get_error() {
Some(err) => match err { Some(err) => match err {
ErrorType::DocumentNotFound => {}, ErrorType::DocumentNotFound => {}
}, },
None => unreachable!("should return an error type"), None => unreachable!("should return an error type"),
} }
@ -131,7 +131,10 @@ impl MoreThanText {
reply.get_data("sess_id").unwrap().to_uuid().unwrap() reply.get_data("sess_id").unwrap().to_uuid().unwrap()
} }
pub fn get_document<S>(&self, sess_id: Uuid, action: ActionType, doc_name: S) -> MTTReply where S: Into<String> { pub fn get_document<S>(&self, sess_id: Uuid, action: ActionType, doc_name: S) -> MTTReply
where
S: Into<String>,
{
let mut msg = Message::new(MsgType::DocumentRequest); let mut msg = Message::new(MsgType::DocumentRequest);
msg.add_data("sess_id", sess_id); msg.add_data("sess_id", sess_id);
msg.add_data("name", doc_name.into()); msg.add_data("name", doc_name.into());
@ -180,6 +183,5 @@ mod mtt {
let id = mtt.validate_session(Some(Uuid::new_v4())); let id = mtt.validate_session(Some(Uuid::new_v4()));
let output = mtt.get_document(id, ActionType::Get, "root".to_string()); let output = mtt.get_document(id, ActionType::Get, "root".to_string());
assert!(output.get_error().is_none()); assert!(output.get_error().is_none());
} }
} }

View File

@ -1,13 +1,13 @@
use axum::{ use axum::{
extract::{Extension, FromRequestParts, State}, extract::{Extension, FromRequestParts, Path, State},
http::{request::Parts, StatusCode}, http::{request::Parts, Method, StatusCode},
response::IntoResponse, response::IntoResponse,
routing::{get, post}, routing::{get, post},
RequestPartsExt, Router, RequestPartsExt, Router,
}; };
use clap::Parser; use clap::Parser;
use morethantext::{ActionType, MoreThanText}; use morethantext::{ActionType, MoreThanText};
use std::convert::Infallible; use std::{collections::HashMap, convert::Infallible};
use tokio::{spawn, sync::mpsc::channel}; use tokio::{spawn, sync::mpsc::channel};
use tower_cookies::{Cookie, CookieManagerLayer, Cookies}; use tower_cookies::{Cookie, CookieManagerLayer, Cookies};
use uuid::Uuid; use uuid::Uuid;
@ -80,10 +80,26 @@ where
} }
} }
async fn mtt_conn(sess_id: SessionID, state: State<MoreThanText>) -> impl IntoResponse { async fn mtt_conn(
sess_id: SessionID,
method: Method,
path: Path<HashMap<String, String>>,
state: State<MoreThanText>,
) -> impl IntoResponse {
let (tx, mut rx) = channel(1); let (tx, mut rx) = channel(1);
let action = match method {
Method::GET => ActionType::Get,
Method::POST => ActionType::Add,
_ => unreachable!("reouter should prevent this"),
};
let doc = match path.get("document") {
Some(result) => result.clone(),
None => "root".to_string(),
};
spawn(async move { spawn(async move {
tx.send(state.get_document(sess_id.0, ActionType::Get, "root")).await.unwrap(); tx.send(state.get_document(sess_id.0, action, doc))
.await
.unwrap();
}); });
let reply = rx.recv().await.unwrap(); let reply = rx.recv().await.unwrap();
let status = match reply.get_error() { let status = match reply.get_error() {
@ -100,8 +116,7 @@ mod servers {
body::Body, body::Body,
http::{ http::{
header::{COOKIE, SET_COOKIE}, header::{COOKIE, SET_COOKIE},
Method, Method, Request,
Request,
}, },
}; };
use tower::ServiceExt; use tower::ServiceExt;
@ -161,23 +176,23 @@ mod servers {
} }
} }
//#[tokio::test] #[tokio::test]
async fn receive_file_not_found() { async fn receive_file_not_found() {
let uri = "/something"; let uri = "/something";
let app = create_app(MoreThanText::new()).await; let app = create_app(MoreThanText::new()).await;
let response = app let response = app
.oneshot( .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())
Request::builder()
.uri(uri)
.body(Body::empty())
.unwrap(),
)
.await .await
.unwrap(); .unwrap();
assert_eq!(response.status(), StatusCode::NOT_FOUND, "'{}' should not exist", uri); assert_eq!(
response.status(),
StatusCode::NOT_FOUND,
"'{}' should not exist",
uri
);
} }
#[tokio::test] //#[tokio::test]
async fn add_new_page() { async fn add_new_page() {
let base = "/something".to_string(); let base = "/something".to_string();
let api = "/api".to_owned() + &base; let api = "/api".to_owned() + &base;
@ -193,16 +208,21 @@ mod servers {
) )
.await .await
.unwrap(); .unwrap();
assert_eq!(response.status(), StatusCode::OK, "failed to post ro {:?}", api); assert_eq!(
response.status(),
StatusCode::OK,
"failed to post ro {:?}",
api
);
let response = app let response = app
.oneshot( .oneshot(Request::builder().uri(&base).body(Body::empty()).unwrap())
Request::builder()
.uri(&base)
.body(Body::empty())
.unwrap(),
)
.await .await
.unwrap(); .unwrap();
assert_eq!(response.status(), StatusCode::OK, "failed to get ro {:?}", base); assert_eq!(
response.status(),
StatusCode::OK,
"failed to get ro {:?}",
base
);
} }
} }