kosyncrs/src/main.rs

69 lines
1.8 KiB
Rust
Raw Normal View History

2023-09-25 15:54:07 +02:00
use axum::{http::StatusCode, routing::get, routing::post, routing::put, Json, Router};
use serde_json::{json, Value};
2023-09-25 15:12:47 +02:00
2023-09-26 10:38:50 +02:00
use serde::Deserialize;
use redis::Commands;
#[derive(Deserialize)]
pub struct CreateUser {
username: String,
password: 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", put(get_progress))
.route("/healthcheck", get(healthcheck));
2023-09-25 15:12:47 +02:00
// run it with hyper on localhost:3000
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.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 10:38:50 +02:00
async fn create_user(Json(payload): Json<CreateUser>) -> (StatusCode, String) {
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 {
return (
StatusCode::PAYMENT_REQUIRED,
"Username is already registered.".to_owned(),
);
}
(StatusCode::CREATED, format!("username = {username}"))
}
2023-09-25 15:54:07 +02:00
async fn auth_user() {}
async fn update_progress() {}
async fn get_progress() {}
async fn healthcheck() -> (StatusCode, Json<Value>) {
2023-09-26 10:38:50 +02:00
(StatusCode::OK, Json(json!({ "state" : "OK" })))
2023-09-25 15:54:07 +02:00
}