Use query not param, anyhow for errors, websocket tx result tracing

This commit is contained in:
2024-12-23 19:22:42 -06:00
parent 61efe6c194
commit b5a5c47ece
4 changed files with 19 additions and 15 deletions

7
Cargo.lock generated
View File

@@ -76,6 +76,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "anyhow"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04"
[[package]] [[package]]
name = "async-trait" name = "async-trait"
version = "0.1.83" version = "0.1.83"
@@ -313,6 +319,7 @@ dependencies = [
name = "dynamic-preauth" name = "dynamic-preauth"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"chrono", "chrono",
"futures-util", "futures-util",
"rand", "rand",

View File

@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.95"
chrono = { version = "0.4.39", features = ["serde"] } chrono = { version = "0.4.39", features = ["serde"] }
futures-util = "0.3.31" futures-util = "0.3.31"
rand = "0.8.5" rand = "0.8.5"

View File

@@ -85,6 +85,7 @@ async fn handle_socket(session_id: u32, websocket: WebSocket) {
let (tx_channel, tx_channel_rx) = mpsc::unbounded_channel(); let (tx_channel, tx_channel_rx) = mpsc::unbounded_channel();
let transmit = UnboundedReceiverStream::new(tx_channel_rx); let transmit = UnboundedReceiverStream::new(tx_channel_rx);
let fut_handle_tx_buffer = transmit.forward(socket_tx).map(|result| { let fut_handle_tx_buffer = transmit.forward(socket_tx).map(|result| {
tracing::debug!("WebSocket send result: {:?}", result);
if let Err(e) = result { if let Err(e) = result {
tracing::error!(error = ?e, "websocket send error"); tracing::error!(error = ?e, "websocket send error");
} }
@@ -179,8 +180,6 @@ pub async fn download(req: &mut Request, res: &mut Response, depot: &mut Depot)
tracing::error!("Error writing body: {}", e); tracing::error!("Error writing body: {}", e);
} }
// TODO: Send the notify message via websocket
res.headers.insert( res.headers.insert(
"Content-Disposition", "Content-Disposition",
HeaderValue::from_str( HeaderValue::from_str(
@@ -203,7 +202,7 @@ pub async fn download(req: &mut Request, res: &mut Response, depot: &mut Depot)
#[handler] #[handler]
pub async fn notify(req: &mut Request, res: &mut Response, depot: &mut Depot) { pub async fn notify(req: &mut Request, res: &mut Response, depot: &mut Depot) {
let key = req.param::<String>("key"); let key = req.query::<String>("key");
match key { match key {
Some(key) => { Some(key) => {
@@ -224,8 +223,8 @@ pub async fn notify(req: &mut Request, res: &mut Response, depot: &mut Depot) {
let message = OutgoingMessage::TokenAlert { let message = OutgoingMessage::TokenAlert {
token: key.unwrap(), token: key.unwrap(),
}; };
session.send_message(message); session.send_message(message);
res.render("Notification sent"); res.render("Notification sent");
} }
None => { None => {

View File

@@ -51,21 +51,18 @@ impl Session {
return self.downloads.last().unwrap(); return self.downloads.last().unwrap();
} }
pub fn send_message(&mut self, message: OutgoingMessage) { pub fn send_message(&mut self, message: OutgoingMessage) -> Result<(), anyhow::Error> {
if self.tx.is_none() { if self.tx.is_none() {
return; return Err(anyhow::anyhow!("Session has no sender"));
} }
// TODO: Error handling, check tx exists // TODO: Error handling
let tx = self.tx.as_ref().unwrap();
let result = tx.send(Ok(Message::text(serde_json::to_string(&message).unwrap())));
let result = self match result {
.tx Ok(_) => return Ok(()),
.as_ref() Err(e) => return Err(anyhow::anyhow!("Error sending message: {}", e)),
.unwrap()
.send(Ok(Message::text(serde_json::to_string(&message).unwrap())));
if let Err(e) = result {
tracing::error!("Failed to initial session state: {}", e);
} }
} }