Skip to content

Commit

Permalink
Some refactoring and a bit more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Threated committed Apr 2, 2024
1 parent 88980d9 commit e24c2ae
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ impl Db {
}
}

pub fn delete_project_db(&mut self, project: String) {
let target = tokens.filter(project_id.eq(&project));
pub fn delete_project_db(&mut self, project: &str) {
let target = tokens.filter(project_id.eq(project));

match diesel::delete(target).execute(&mut self.0) {
Ok(_) => {
Expand Down
18 changes: 7 additions & 11 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use uuid::Uuid;
pub async fn send_token_registration_request(
db: Db,
token_params: TokenParams,
) -> Result<OpalResponse<String>, anyhow::Error> {
) -> Result<(), anyhow::Error> {
let token_name = Uuid::new_v4().to_string();
let task = create_and_send_task_request(
OpalRequestType::CREATE,
Expand All @@ -39,9 +39,7 @@ pub async fn send_token_registration_request(
info!("Created token task {task:#?}");

tokio::task::spawn(save_tokens_from_beam(db, task, token_params, token_name));
Ok(OpalResponse::Ok {
response: "OK".to_string(),
})
Ok(())
}

pub async fn send_token_from_db(token_params: TokenParams, token_name: String, token: String) {
Expand All @@ -58,7 +56,7 @@ pub async fn send_token_from_db(token_params: TokenParams, token_name: String, t

pub async fn remove_project_and_tokens_request(
mut db: Db,
token_params: ProjectQueryParams,
token_params: &ProjectQueryParams,
) -> Result<OpalResponse<String>, anyhow::Error> {
let task = create_and_send_task_request(
OpalRequestType::DELETE,
Expand All @@ -73,7 +71,7 @@ pub async fn remove_project_and_tokens_request(

match remove_project_and_tokens_from_beam(task).await {
Ok(response) => {
db.delete_project_db(token_params.project_id);
db.delete_project_db(&token_params.project_id);
Ok(response)
}
Err(e) => Err(e),
Expand All @@ -82,7 +80,7 @@ pub async fn remove_project_and_tokens_request(

pub async fn remove_tokens_request(
mut db: Db,
token_params: TokensQueryParams,
token_params: &TokensQueryParams,
) -> Result<OpalResponse<String>, anyhow::Error> {
let token_name_result = db.get_token_name(
token_params.user_id.clone(),
Expand Down Expand Up @@ -120,7 +118,7 @@ pub async fn remove_tokens_request(
pub async fn refresh_token_request(
mut db: Db,
token_params: TokenParams,
) -> Result<OpalResponse<String>, anyhow::Error> {
) -> Result<(), anyhow::Error> {
let token_name = match db.get_token_name(
token_params.user_id.clone(),
token_params.project_id.clone(),
Expand Down Expand Up @@ -159,9 +157,7 @@ pub async fn refresh_token_request(
token_params,
token_name.clone(),
));
Ok(OpalResponse::Ok {
response: "OK".to_string(),
})
Ok(())
}

pub async fn fetch_project_tables_names_request(
Expand Down
4 changes: 2 additions & 2 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ pub struct TokenStatus<'a> {
pub user_id: &'a str,
}

#[derive(Deserialize)]
#[derive(Deserialize, Debug)]
pub struct TokensQueryParams {
pub user_id: String,
pub bk: String,
pub project_id: String,
}

#[derive(Deserialize)]
#[derive(Deserialize, Debug)]
pub struct ProjectQueryParams {
pub bk: String,
pub project_id: String,
Expand Down
46 changes: 15 additions & 31 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,11 @@ use serde_json::json;
use tracing::{error, warn};

async fn create_token(db: Db, token_params: Json<TokenParams>) -> impl IntoResponse {
match send_token_registration_request(db, token_params.0).await {
Ok(OpalResponse::Ok { .. }) => StatusCode::OK.into_response(),
Ok(OpalResponse::Err {
status_code,
error_message,
}) => {
let status = StatusCode::from_u16(status_code as u16)
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
(status, Json(json!({ "error": error_message }))).into_response()
}
Err(e) => {
error!("Unhandled error: {e:?}");
StatusCode::INTERNAL_SERVER_ERROR.into_response()
}
if let Err(e) = send_token_registration_request(db, token_params.0).await {
error!("Unhandled error: {e:?}");
StatusCode::INTERNAL_SERVER_ERROR
} else {
StatusCode::OK
}
}

Expand Down Expand Up @@ -67,31 +58,23 @@ async fn generate_script(mut db: Db, script_params: Json<TokenParams>) -> impl I
}
}

async fn refresh_token(db: Db, token_params: Json<TokenParams>) -> impl IntoResponse {
match refresh_token_request(db, token_params.0).await {
Ok(OpalResponse::Ok { .. }) => StatusCode::OK.into_response(),
Ok(OpalResponse::Err {
status_code,
error_message,
}) => {
let status = StatusCode::from_u16(status_code as u16)
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
(status, Json(json!({ "error": error_message }))).into_response()
}
Err(e) => {
error!("Unhandled error: {e:?}");
StatusCode::INTERNAL_SERVER_ERROR.into_response()
}
async fn refresh_token(db: Db, token_params: Json<TokenParams>) -> StatusCode {
if let Err(e) = refresh_token_request(db, token_params.0).await {
error!("Unhandled error: {e:?}");
StatusCode::INTERNAL_SERVER_ERROR
} else {
StatusCode::OK
}
}

async fn remove_project_and_token(db: Db, query: Query<ProjectQueryParams>) -> impl IntoResponse {
match remove_project_and_tokens_request(db, query.0).await {
match remove_project_and_tokens_request(db, &query.0).await {
Ok(OpalResponse::Ok { .. }) => StatusCode::OK.into_response(),
Ok(OpalResponse::Err {
status_code,
error_message,
}) => {
warn!(?query, ?error_message, ?status_code, "Got error while project");
let status = StatusCode::from_u16(status_code as u16)
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
(status, Json(json!({ "error": error_message }))).into_response()
Expand All @@ -104,12 +87,13 @@ async fn remove_project_and_token(db: Db, query: Query<ProjectQueryParams>) -> i
}

async fn remove_tokens(db: Db, query: Query<TokensQueryParams>) -> impl IntoResponse {
match remove_tokens_request(db, query.0).await {
match remove_tokens_request(db, &query.0).await {
Ok(OpalResponse::Ok { .. }) => StatusCode::OK.into_response(),
Ok(OpalResponse::Err {
status_code,
error_message,
}) => {
warn!(?query, ?error_message, ?status_code, "Got error while removing tokens");
let status = StatusCode::from_u16(status_code as u16)
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
(status, Json(json!({ "error": error_message }))).into_response()
Expand Down

0 comments on commit e24c2ae

Please sign in to comment.