kosyncrs/src/main.rs

140 lines
3.6 KiB
Rust
Raw Normal View History

2023-09-26 11:09:00 +02:00
use axum::{
http::HeaderMap, http::StatusCode, routing::get, routing::post, routing::put, Json, Router,
};
2023-09-25 15:54:07 +02:00
2023-09-26 10:38:50 +02:00
use serde::Deserialize;
use redis::Commands;
use std::time::{SystemTime, UNIX_EPOCH};
2023-09-26 10:38:50 +02:00
#[derive(Deserialize)]
2023-09-26 11:09:00 +02:00
pub struct User {
2023-09-26 10:38:50 +02:00
username: String,
password: String,
}
#[derive(Deserialize)]
pub struct Progress {
document: String,
progress: String,
percentage: String,
device: String,
device_id: String,
}
2023-09-25 15:12:47 +02:00
#[tokio::main]
async fn main() {
// build our application with a single route
2023-09-25 15:54:07 +02:00
let app = Router::new()
.route("/", get(root))
.route("/users/create", post(create_user))
.route("/users/auth", get(auth_user))
.route("/syncs/progress", put(update_progress))
.route("/syncs/progress/:document", get(get_progress))
2023-09-25 15:54:07 +02:00
.route("/healthcheck", get(healthcheck));
2023-09-25 15:12:47 +02:00
// run it with hyper on localhost:3000
2023-09-26 12:49:33 +02:00
axum::Server::bind(&"0.0.0.0:3003".parse().unwrap())
2023-09-25 15:12:47 +02:00
.serve(app.into_make_service())
.await
.unwrap();
}
2023-09-25 15:54:07 +02:00
async fn root() -> &'static str {
2023-09-25 16:13:14 +02:00
"KOreader sync server"
2023-09-25 15:54:07 +02:00
}
2023-09-26 12:48:53 +02:00
async fn create_user(Json(payload): Json<User>) -> (StatusCode, String) {
2023-09-26 10:38:50 +02:00
let client = redis::Client::open("redis://127.0.0.1/").unwrap();
let mut con = client.get_connection().unwrap();
let username = payload.username;
let password = payload.password;
let user_key = format!("user:{username}:key");
let does_exist: bool = con.exists(&user_key).unwrap();
if does_exist == false {
let _: () = con.set(&user_key, password).unwrap();
} else {
2023-09-26 12:48:53 +02:00
return (
StatusCode::PAYMENT_REQUIRED,
"Username is already registered.".to_owned(),
);
2023-09-26 10:38:50 +02:00
}
2023-09-26 12:48:53 +02:00
(StatusCode::CREATED, format!("username = {username}"))
2023-09-26 10:38:50 +02:00
}
2023-09-25 15:54:07 +02:00
fn authorize(username: &str, password: &str) -> bool {
2023-09-26 11:09:00 +02:00
let client = redis::Client::open("redis://127.0.0.1/").unwrap();
let mut con = client.get_connection().unwrap();
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 {
2023-09-26 11:09:00 +02:00
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;
2023-09-26 11:09:00 +02:00
}
2023-09-26 12:40:52 +02:00
StatusCode::OK
2023-09-26 11:09:00 +02:00
}
2023-09-25 15:54:07 +02:00
async fn update_progress(headers: HeaderMap, Json(payload): Json<Progress>) -> StatusCode {
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
}
2023-09-25 15:54:07 +02:00
async fn get_progress() {}
2023-09-26 10:42:24 +02:00
async fn healthcheck() -> (StatusCode, String) {
2023-09-26 14:35:48 +02:00
(StatusCode::OK, "{ \"state\" : \"OK\" }".to_owned())
2023-09-25 15:54:07 +02:00
}