WIP: get_progress

This commit is contained in:
Martin Brodbeck 2023-09-26 17:14:04 +02:00
parent c8be7cdeee
commit 5664d933a5

View file

@ -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<Progress>) -> StatusCode {
async fn update_progress(headers: HeaderMap, Json(payload): Json<UpdateProgress>) -> 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<Progress>) -> S
StatusCode::OK
}
async fn get_progress(headers: HeaderMap, Path(document): Path<String>) -> StatusCode {
async fn get_progress(
headers: HeaderMap,
Path(document): Path<String>,
) -> (StatusCode, Json<Value>) {
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<String>) -> Statu
let doc_key = format!("user:{username}:document:{document}");
let values : Vec<String> = con.hget(doc_key, &["progress", "percentage"]).unwrap();
let values: Vec<String> = 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) {