Skip to content

Commit

Permalink
Merge pull request #119 from L2-Technology/single-init
Browse files Browse the repository at this point in the history
ensure init instance only works once
  • Loading branch information
johncantrell97 authored Sep 23, 2022
2 parents 055d928 + 3432967 commit 2c902f9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
26 changes: 16 additions & 10 deletions senseicore/src/services/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,16 +360,22 @@ impl AdminService {
username,
passphrase,
} => {
let root_token = self.database.create_root_access_token().await.unwrap();
let _user = self
.database
.create_user(username, passphrase)
.await
.unwrap();

Ok(AdminResponse::CreateAdmin {
token: root_token.token,
})
let root_token = self.database.get_root_access_token().await.unwrap();
match root_token {
Some(_) => Err(Error::Generic("Instance already initialized".to_string())),
None => {
let root_token = self.database.create_root_access_token().await.unwrap();
let _user = self
.database
.create_user(username, passphrase)
.await
.unwrap();

Ok(AdminResponse::CreateAdmin {
token: root_token.token,
})
}
}
}
AdminRequest::StartNode { pubkey, passphrase } => {
let node = self.database.get_node_by_pubkey(&pubkey).await?;
Expand Down
31 changes: 23 additions & 8 deletions src/http/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::sync::Arc;

use axum::{
extract::{Extension, Query},
response::IntoResponse,
routing::{delete, get, post},
Json, Router,
};
Expand All @@ -22,7 +23,7 @@ use serde_json::{json, Value};

use senseicore::{
services::{
admin::{AdminRequest, AdminResponse, AdminService, NodeCreateInfo},
admin::{AdminRequest, AdminResponse, AdminService, Error, NodeCreateInfo},
PaginationRequest,
},
utils,
Expand Down Expand Up @@ -777,12 +778,18 @@ pub async fn init_sensei(
Extension(admin_service): Extension<Arc<AdminService>>,
cookies: Cookies,
Json(payload): Json<Value>,
) -> Result<Json<AdminResponse>, StatusCode> {
) -> impl IntoResponse {
let params: Result<CreateAdminParams, _> = serde_json::from_value(payload);

let request = match params {
Ok(params) => Ok(params.into()),
Err(_) => Err(StatusCode::UNPROCESSABLE_ENTITY),
}?;
Ok(params) => params.into(),
Err(_) => {
return (
StatusCode::UNPROCESSABLE_ENTITY,
Json(json!({"error": "invalid params"})),
)
}
};

match admin_service.call(request).await {
Ok(response) => match response {
Expand All @@ -792,11 +799,19 @@ pub async fn init_sensei(
.finish();

cookies.add(token_cookie);
Ok(Json(AdminResponse::CreateAdmin { token }))
(StatusCode::OK, Json(json!({ "token": token })))
}
_ => Err(StatusCode::UNPROCESSABLE_ENTITY),
_ => (
StatusCode::UNPROCESSABLE_ENTITY,
Json(json!({"error": "unexpected error"})),
),
},
Err(err) => match err {
Error::Generic(msg) => (
StatusCode::UNPROCESSABLE_ENTITY,
Json(json!({ "error": msg })),
),
},
Err(err) => Ok(Json(AdminResponse::Error(err))),
}
}

Expand Down

0 comments on commit 2c902f9

Please sign in to comment.