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

View File

@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0.95"
chrono = { version = "0.4.39", features = ["serde"] }
futures-util = "0.3.31"
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 transmit = UnboundedReceiverStream::new(tx_channel_rx);
let fut_handle_tx_buffer = transmit.forward(socket_tx).map(|result| {
tracing::debug!("WebSocket send result: {:?}", result);
if let Err(e) = result {
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);
}
// TODO: Send the notify message via websocket
res.headers.insert(
"Content-Disposition",
HeaderValue::from_str(
@@ -203,7 +202,7 @@ pub async fn download(req: &mut Request, res: &mut Response, depot: &mut Depot)
#[handler]
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 {
Some(key) => {
@@ -224,8 +223,8 @@ pub async fn notify(req: &mut Request, res: &mut Response, depot: &mut Depot) {
let message = OutgoingMessage::TokenAlert {
token: key.unwrap(),
};
session.send_message(message);
res.render("Notification sent");
}
None => {

View File

@@ -51,21 +51,18 @@ impl Session {
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() {
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
.tx
.as_ref()
.unwrap()
.send(Ok(Message::text(serde_json::to_string(&message).unwrap())));
if let Err(e) = result {
tracing::error!("Failed to initial session state: {}", e);
match result {
Ok(_) => return Ok(()),
Err(e) => return Err(anyhow::anyhow!("Error sending message: {}", e)),
}
}