Compare commits

..

No commits in common. "19166d723c3667c38a6e3c8c1ecdee6d3300ac31" and "320cd84774b6dd899a4f2422f5469b4c398d70e0" have entirely different histories.

View file

@ -2,27 +2,18 @@ use axum::{
http::HeaderMap, http::StatusCode, routing::get, routing::post, routing::put, Json, Router, http::HeaderMap, http::StatusCode, routing::get, routing::post, routing::put, Json, Router,
}; };
use serde_json::{json, Value};
use serde::Deserialize; use serde::Deserialize;
use redis::Commands; use redis::Commands;
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct User { pub struct User {
username: String, username: String,
password: String, password: String,
} }
#[derive(Deserialize)]
pub struct Progress {
document: String,
progress: String,
percentage: String,
device: String,
device_id: String,
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// build our application with a single route // build our application with a single route
@ -31,7 +22,7 @@ async fn main() {
.route("/users/create", post(create_user)) .route("/users/create", post(create_user))
.route("/users/auth", get(auth_user)) .route("/users/auth", get(auth_user))
.route("/syncs/progress", put(update_progress)) .route("/syncs/progress", put(update_progress))
.route("/syncs/progress/:document", get(get_progress)) .route("/syncs/progress/:document", put(get_progress))
.route("/healthcheck", get(healthcheck)); .route("/healthcheck", get(healthcheck));
// run it with hyper on localhost:3000 // run it with hyper on localhost:3000
@ -68,33 +59,14 @@ async fn create_user(Json(payload): Json<User>) -> (StatusCode, String) {
(StatusCode::CREATED, format!("username = {username}")) (StatusCode::CREATED, format!("username = {username}"))
} }
fn authorize(username: &str, password: &str) -> bool { async fn auth_user(headers: HeaderMap) -> StatusCode {
let client = redis::Client::open("redis://127.0.0.1/").unwrap(); let client = redis::Client::open("redis://127.0.0.1/").unwrap();
let mut con = client.get_connection().unwrap(); let mut con = client.get_connection().unwrap();
let username = headers["x-auth-user"].to_str().unwrap_or("");
let password = headers["x-auth-key"].to_str().unwrap_or("");
if username.is_empty() || password.is_empty() { if username.is_empty() || password.is_empty() {
return false;
}
let user_key = format!("user:{username}:key");
let redis_pw: String = con.get(&user_key).unwrap();
if password != redis_pw {
return false;
}
true
}
async fn auth_user(headers: HeaderMap) -> StatusCode {
//let client = redis::Client::open("redis://127.0.0.1/").unwrap();
//let mut con = client.get_connection().unwrap();
let username = headers["x-auth-user"].to_str().unwrap_or("");
let password = headers["x-auth-key"].to_str().unwrap_or("");
/*if username.is_empty() || password.is_empty() {
return StatusCode::UNAUTHORIZED; return StatusCode::UNAUTHORIZED;
} }
@ -104,48 +76,12 @@ async fn auth_user(headers: HeaderMap) -> StatusCode {
if password != redis_pw { if password != redis_pw {
return StatusCode::UNAUTHORIZED; return StatusCode::UNAUTHORIZED;
}*/
if authorize(&username, &password) == false {
return StatusCode::UNAUTHORIZED;
} }
StatusCode::OK StatusCode::OK
} }
async fn update_progress(headers: HeaderMap, Json(payload): Json<Progress>) -> StatusCode { async fn update_progress() {}
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;
}
let client = redis::Client::open("redis://127.0.0.1/").unwrap();
let mut con = client.get_connection().unwrap();
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
let document = payload.document;
let doc_key = format!("user:{username}:document:{document}");
let _: () = con
.hset_multiple(
&doc_key,
&[
("percentage", &payload.percentage),
("progress", &payload.progress),
("device", &payload.device),
("device_id", &payload.device_id),
("timestamp", &timestamp.to_string()),
],
)
.unwrap();
StatusCode::OK
}
async fn get_progress() {} async fn get_progress() {}