From e24c2ae87da396261a0e890af075d24c1d02ff69 Mon Sep 17 00:00:00 2001 From: janskiba Date: Tue, 2 Apr 2024 16:26:05 +0200 Subject: [PATCH] Some refactoring and a bit more logging --- src/db.rs | 4 ++-- src/handlers.rs | 18 +++++++----------- src/models.rs | 4 ++-- src/routes.rs | 46 +++++++++++++++------------------------------- 4 files changed, 26 insertions(+), 46 deletions(-) diff --git a/src/db.rs b/src/db.rs index d85bbd9..da06a2f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -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(_) => { diff --git a/src/handlers.rs b/src/handlers.rs index 122cedf..100f6a9 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -25,7 +25,7 @@ use uuid::Uuid; pub async fn send_token_registration_request( db: Db, token_params: TokenParams, -) -> Result, anyhow::Error> { +) -> Result<(), anyhow::Error> { let token_name = Uuid::new_v4().to_string(); let task = create_and_send_task_request( OpalRequestType::CREATE, @@ -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) { @@ -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, anyhow::Error> { let task = create_and_send_task_request( OpalRequestType::DELETE, @@ -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), @@ -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, anyhow::Error> { let token_name_result = db.get_token_name( token_params.user_id.clone(), @@ -120,7 +118,7 @@ pub async fn remove_tokens_request( pub async fn refresh_token_request( mut db: Db, token_params: TokenParams, -) -> Result, anyhow::Error> { +) -> Result<(), anyhow::Error> { let token_name = match db.get_token_name( token_params.user_id.clone(), token_params.project_id.clone(), @@ -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( diff --git a/src/models.rs b/src/models.rs index 3154997..d4742e1 100644 --- a/src/models.rs +++ b/src/models.rs @@ -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, diff --git a/src/routes.rs b/src/routes.rs index 7b97ce0..9d4ba3c 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -16,20 +16,11 @@ use serde_json::json; use tracing::{error, warn}; async fn create_token(db: Db, token_params: Json) -> 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 } } @@ -67,31 +58,23 @@ async fn generate_script(mut db: Db, script_params: Json) -> impl I } } -async fn refresh_token(db: Db, token_params: Json) -> 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) -> 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) -> 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() @@ -104,12 +87,13 @@ async fn remove_project_and_token(db: Db, query: Query) -> i } async fn remove_tokens(db: Db, query: Query) -> 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()