Skip to content

Commit

Permalink
feat(config): add API route set_config (#1144)
Browse files Browse the repository at this point in the history
  • Loading branch information
NishantJoshi00 authored May 15, 2023
1 parent 54ff02d commit f31926b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
18 changes: 18 additions & 0 deletions crates/router/src/core/configs.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
use error_stack::ResultExt;

use crate::{
core::errors::{self, utils::StorageErrorExt, RouterResponse},
db::StorageInterface,
services::ApplicationResponse,
types::{api, transformers::ForeignInto},
};

pub async fn set_config(
store: &dyn StorageInterface,
config: api::Config,
) -> RouterResponse<api::Config> {
let config = store
.insert_config(storage_models::configs::ConfigNew {
key: config.key,
config: config.value,
})
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Unknown error, while setting config key")?;

Ok(ApplicationResponse::Json(config.foreign_into()))
}

pub async fn read_config(store: &dyn StorageInterface, key: &str) -> RouterResponse<api::Config> {
let config = store
.find_config_by_key_cached(key)
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/routes/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ impl Configs {
pub fn server(config: AppState) -> Scope {
web::scope("/configs")
.app_data(web::Data::new(config))
.service(web::resource("/").route(web::post().to(config_key_create)))
.service(
web::resource("/{key}")
.route(web::get().to(config_key_retrieve))
Expand Down
20 changes: 20 additions & 0 deletions crates/router/src/routes/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ use crate::{
types::api as api_types,
};

#[instrument(skip_all, fields(flow = ?Flow::CreateConfigKey))]
pub async fn config_key_create(
state: web::Data<AppState>,
req: HttpRequest,
json_payload: web::Json<api_types::Config>,
) -> impl Responder {
let flow = Flow::CreateConfigKey;
let payload = json_payload.into_inner();

api::server_wrap(
flow,
state.get_ref(),
&req,
payload,
|state, _, data| configs::set_config(&*state.store, data),
&auth::AdminApiAuth,
)
.await
}

#[instrument(skip_all, fields(flow = ?Flow::ConfigKeyFetch))]
pub async fn config_key_retrieve(
state: web::Data<AppState>,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/types/api/configs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Clone, serde::Serialize, Debug)]
#[derive(Clone, serde::Serialize, Debug, serde::Deserialize)]
pub struct Config {
pub key: String,
pub value: String,
Expand Down
2 changes: 2 additions & 0 deletions crates/router_env/src/logger/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ pub enum Flow {
RetrieveFile,
/// Dispute Evidence submission flow
DisputesEvidenceSubmit,
/// Create Config Key flow
CreateConfigKey,
/// Attach Dispute Evidence flow
AttachDisputeEvidence,
}
Expand Down

0 comments on commit f31926b

Please sign in to comment.