From 5664d933a5b5b1d0c42a8acc7b8c316058ecabc1 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Tue, 26 Sep 2023 17:14:04 +0200 Subject: [PATCH] WIP: get_progress --- src/main.rs | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5816dc4..832bce8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,9 @@ use axum::{ use serde::Deserialize; use redis::Commands; +use serde::Serialize; +use serde_json::json; +use serde_json::Value; use std::time::{SystemTime, UNIX_EPOCH}; @@ -16,7 +19,7 @@ pub struct User { } #[derive(Deserialize)] -pub struct Progress { +pub struct UpdateProgress { document: String, progress: String, percentage: f32, @@ -24,6 +27,16 @@ pub struct Progress { device_id: String, } +#[derive(Deserialize, Serialize)] +pub struct GetProgress { + document: String, + progress: String, + percentage: f32, + device: String, + device_id: String, + timestamp: u64, +} + #[tokio::main] async fn main() { // build our application with a single route @@ -99,7 +112,7 @@ async fn auth_user(headers: HeaderMap) -> StatusCode { StatusCode::OK } -async fn update_progress(headers: HeaderMap, Json(payload): Json) -> StatusCode { +async fn update_progress(headers: HeaderMap, Json(payload): Json) -> StatusCode { let username = headers["x-auth-user"].to_str().unwrap_or(""); let password = headers["x-auth-key"].to_str().unwrap_or(""); @@ -133,12 +146,15 @@ async fn update_progress(headers: HeaderMap, Json(payload): Json) -> S StatusCode::OK } -async fn get_progress(headers: HeaderMap, Path(document): Path) -> StatusCode { +async fn get_progress( + headers: HeaderMap, + Path(document): Path, +) -> (StatusCode, Json) { let username = headers["x-auth-user"].to_str().unwrap_or(""); let password = headers["x-auth-key"].to_str().unwrap_or(""); if authorize(username, password) == false { - return StatusCode::UNAUTHORIZED; + return (StatusCode::UNAUTHORIZED, Json(json!(""))); } let client = redis::Client::open("redis://127.0.0.1/").unwrap(); @@ -146,11 +162,24 @@ async fn get_progress(headers: HeaderMap, Path(document): Path) -> Statu let doc_key = format!("user:{username}:document:{document}"); - let values : Vec = con.hget(doc_key, &["progress", "percentage"]).unwrap(); + let values: Vec = con + .hget( + doc_key, + &["percentage", "progress", "device", "device_id", "timestamp"], + ) + .unwrap(); println!("{values:?}"); + let res = GetProgress { + percentage: values[0].parse().unwrap(), + progress: values[1].clone(), + device: values[2].clone(), + device_id: values[3].clone(), + timestamp: values[4].parse().unwrap(), + document, + }; - StatusCode::OK + (StatusCode::OK, Json(json!(res))) } async fn healthcheck() -> (StatusCode, String) {