Skip to content

Commit

Permalink
fix: service info group, artifact and version, and add flexibility in…
Browse files Browse the repository at this point in the history
… configuration
  • Loading branch information
mmalenic committed Dec 18, 2024
1 parent 77d862b commit 0916b0c
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 286 deletions.
2 changes: 1 addition & 1 deletion htsget-actix/src/handlers/service_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn get_service_info_json<H: HtsGet + Clone + Send + Sync + 'static>(
PrettyJson(get_base_service_info_json(
endpoint,
app_state.htsget.clone(),
&app_state.config_service_info,
app_state.config_service_info.clone(),
))
}

Expand Down
2 changes: 1 addition & 1 deletion htsget-axum/src/handlers/service_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn get_service_info_json<H: HtsGet + Send + Sync + 'static>(
ErasedJson::pretty(get_base_service_info_json(
endpoint,
app_state.htsget,
&app_state.service_info,
app_state.service_info,
))
}

Expand Down
80 changes: 80 additions & 0 deletions htsget-config/src/config/service_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//! Service info configuration.
//!
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;
use std::collections::HashMap;

/// Service info config.
#[derive(Serialize, Debug, Clone, Default, PartialEq, Eq)]
#[serde(default)]
pub struct ServiceInfo(HashMap<String, Value>);

impl ServiceInfo {
/// Create a service info.
pub fn new(fields: HashMap<String, Value>) -> Self {
Self(fields)
}

/// Get the inner value.
pub fn into_inner(self) -> HashMap<String, Value> {
self.0
}
}

impl AsRef<HashMap<String, Value>> for ServiceInfo {
fn as_ref(&self) -> &HashMap<String, Value> {
&self.0
}
}

impl<'de> Deserialize<'de> for ServiceInfo {
fn deserialize<D>(deserializer: D) -> Result<ServiceInfo, D::Error>
where
D: Deserializer<'de>,
{
let fields: HashMap<String, Value> = HashMap::<String, Value>::deserialize(deserializer)?
.into_iter()
.map(|(key, value)| (key.to_lowercase(), value))
.collect();

let err_msg = |invalid_key| format!("reserved service info field `{}`", invalid_key);

if fields.contains_key("type") {
return Err(Error::custom(err_msg("type")));
}

if fields.contains_key("htsget") {
return Err(Error::custom(err_msg("htsget")));
}

Ok(ServiceInfo::new(fields))
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::config::tests::test_serialize_and_deserialize;
use crate::config::Config;
use serde_json::json;

#[test]
fn service_info() {
test_serialize_and_deserialize(
r#"
service_info.environment = "dev"
service_info.organization = { name = "name", url = "https://example.com/" }
"#,
HashMap::from_iter(vec![
("environment".to_string(), json!("dev")),
(
"organization".to_string(),
json!({ "name": "name", "url": "https://example.com/" }),
),
]),
|result: Config| result.service_info.0,
);
}
}
180 changes: 0 additions & 180 deletions htsget-config/src/storage/local.rs

This file was deleted.

Loading

0 comments on commit 0916b0c

Please sign in to comment.