use std::collections::HashMap; use std::fs; use std::sync::RwLock; use rocket::serde::json::Json; use rocket::{post, routes, State}; use uuid::Uuid; mod common; use common::{Entry, Index, Key, Params, PartySignup}; #[post("/get", format = "json", data = "")] fn get( db_mtx: &State>>, request: Json, ) -> Json> { let index: Index = request.0; let hm = db_mtx.read().unwrap(); match hm.get(&index.key) { Some(v) => { let entry = Entry { key: index.key, value: v.clone(), }; Json(Ok(entry)) } None => Json(Err(())), } } #[post("/set", format = "json", data = "")] fn set(db_mtx: &State>>, request: Json) -> Json> { let entry: Entry = request.0; let mut hm = db_mtx.write().unwrap(); hm.insert(entry.key.clone(), entry.value); Json(Ok(())) } #[post("/signupkeygen", format = "json")] fn signup_keygen(db_mtx: &State>>) -> Json> { let data = fs::read_to_string("params.json") .expect("Unable to read params, make sure config file is present in the same folder "); let params: Params = serde_json::from_str(&data).unwrap(); let parties = params.parties.parse::().unwrap(); let key = "signup-keygen".to_string(); let party_signup = { let hm = db_mtx.read().unwrap(); let value = hm.get(&key).unwrap(); let client_signup: PartySignup = serde_json::from_str(value).unwrap(); if client_signup.number < parties { PartySignup { number: client_signup.number + 1, uuid: client_signup.uuid, } } else { PartySignup { number: 1, uuid: Uuid::new_v4().to_string(), } } }; let mut hm = db_mtx.write().unwrap(); hm.insert(key, serde_json::to_string(&party_signup).unwrap()); Json(Ok(party_signup)) } #[post("/signupsign", format = "json")] fn signup_sign(db_mtx: &State>>) -> Json> { //read parameters: let data = fs::read_to_string("params.json") .expect("Unable to read params, make sure config file is present in the same folder "); let params: Params = serde_json::from_str(&data).unwrap(); let threshold = params.threshold.parse::().unwrap(); let key = "signup-sign".to_string(); let party_signup = { let hm = db_mtx.read().unwrap(); let value = hm.get(&key).unwrap(); let client_signup: PartySignup = serde_json::from_str(value).unwrap(); if client_signup.number < threshold + 1 { PartySignup { number: client_signup.number + 1, uuid: client_signup.uuid, } } else { PartySignup { number: 1, uuid: Uuid::new_v4().to_string(), } } }; let mut hm = db_mtx.write().unwrap(); hm.insert(key, serde_json::to_string(&party_signup).unwrap()); Json(Ok(party_signup)) } #[tokio::main] async fn main() { // let mut my_config = Config::development(); // my_config.set_port(18001); let db: HashMap = HashMap::new(); let db_mtx = RwLock::new(db); //rocket::custom(my_config).mount("/", routes![get, set]).manage(db_mtx).launch(); ///////////////////////////////////////////////////////////////// //////////////////////////init signups:////////////////////////// ///////////////////////////////////////////////////////////////// let keygen_key = "signup-keygen".to_string(); let sign_key = "signup-sign".to_string(); let uuid_keygen = Uuid::new_v4().to_string(); let uuid_sign = Uuid::new_v4().to_string(); let party1 = 0; let party_signup_keygen = PartySignup { number: party1, uuid: uuid_keygen, }; let party_signup_sign = PartySignup { number: party1, uuid: uuid_sign, }; { let mut hm = db_mtx.write().unwrap(); hm.insert( keygen_key, serde_json::to_string(&party_signup_keygen).unwrap(), ); hm.insert(sign_key, serde_json::to_string(&party_signup_sign).unwrap()); } ///////////////////////////////////////////////////////////////// rocket::build() .mount("/", routes![get, set, signup_keygen, signup_sign]) .manage(db_mtx) .launch() .await .unwrap(); }