feat: add update

This commit is contained in:
2022-07-24 18:14:56 +08:00
parent d0e1c503ca
commit 3c8e12a8d2

View File

@@ -71,6 +71,7 @@ async fn response_requests(
) -> Result<Response<Body>> {
match (req.method(), req.uri().path()) {
(&Method::POST, "/init") => init(req).await,
(&Method::POST, "/update") => update().await,
(&Method::POST, "/decrypt") => decrypt(req).await,
(&Method::POST, "/encrypt") => encrypt(req).await,
(&Method::GET, "/status") => status().await,
@@ -122,6 +123,17 @@ fn init_instance(db: &str) -> XResult<bool> {
}
}
fn update_instance_rsa_key_pair() -> XResult<bool> {
let mut startup_rw_lock = STATUP_RW_LOCK.write().expect("Lock write startup rw lock error");
match &mut *startup_rw_lock {
Some(k) => {
k.instance_rsa_key_pair = jose::generate_rsa_key(4096)?;
Ok(true)
}
None => Ok(false),
}
}
#[derive(Serialize, Deserialize)]
struct MultipleViewValue {
value: Option<String>,
@@ -178,6 +190,17 @@ async fn inner_encrypt(req: Request<Body>) -> XResult<(StatusCode, Value)> {
})
}
async fn update() -> Result<Response<Body>> {
do_response!(inner_update().await)
}
async fn inner_update() -> XResult<(StatusCode, Value)> {
let update = update_instance_rsa_key_pair()?;
Ok((StatusCode::OK, json!({
"update": update,
})))
}
async fn init(req: Request<Body>) -> Result<Response<Body>> {
do_response!(inner_init(req).await)
}