Skip to content

Commit

Permalink
feat: [#672] improve metadata section in configuration
Browse files Browse the repository at this point in the history
```toml
[metadata]
app = "torrust-index"
purpose = "configuration"
schema_version = "2.0.0"
```
  • Loading branch information
josecelano committed Aug 2, 2024
1 parent 330f490 commit 5bd611b
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 21 deletions.
5 changes: 4 additions & 1 deletion share/default/config/index.container.mysql.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
version = "2"
[metadata]
app = "torrust-index"
purpose = "configuration"
schema_version = "2.0.0"

[logging]
#threshold = "off"
Expand Down
5 changes: 4 additions & 1 deletion share/default/config/index.container.sqlite3.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
version = "2"
[metadata]
app = "torrust-index"
purpose = "configuration"
schema_version = "2.0.0"

[logging]
#threshold = "off"
Expand Down
5 changes: 4 additions & 1 deletion share/default/config/index.development.sqlite3.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
version = "2"
[metadata]
app = "torrust-index"
purpose = "configuration"
schema_version = "2.0.0"

[logging]
#threshold = "off"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
version = "2"
[metadata]
app = "torrust-index"
purpose = "configuration"
schema_version = "2.0.0"

[logging]
#threshold = "off"
Expand Down
5 changes: 4 additions & 1 deletion share/default/config/index.public.e2e.container.mysql.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
version = "2"
[metadata]
app = "torrust-index"
purpose = "configuration"
schema_version = "2.0.0"

[logging]
#threshold = "off"
Expand Down
5 changes: 4 additions & 1 deletion share/default/config/index.public.e2e.container.sqlite3.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
version = "2"
[metadata]
app = "torrust-index"
purpose = "configuration"
schema_version = "2.0.0"

[logging]
#threshold = "off"
Expand Down
62 changes: 49 additions & 13 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub type Threshold = v2::logging::Threshold;
pub type Website = v2::website::Website;

/// Configuration version
const VERSION_2: &str = "2";
const VERSION_2: &str = "2.0.0";

/// Prefix for env vars that overwrite configuration options.
const CONFIG_OVERRIDE_PREFIX: &str = "TORRUST_INDEX_CONFIG_OVERRIDE_";
Expand All @@ -64,55 +64,88 @@ pub const ENV_VAR_CONFIG_TOML: &str = "TORRUST_INDEX_CONFIG_TOML";
/// The `index.toml` file location.
pub const ENV_VAR_CONFIG_TOML_PATH: &str = "TORRUST_INDEX_CONFIG_TOML_PATH";

pub const LATEST_VERSION: &str = "2";
pub const LATEST_VERSION: &str = "2.0.0";

/// Info about the configuration specification.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display, Clone)]
#[display(fmt = "Metadata(app: {app}, purpose: {purpose}, schema_version: {schema_version})")]
pub struct Metadata {
#[serde(default = "Metadata::default_version")]
/// The application this configuration is valid for.
#[serde(default = "Metadata::default_app")]
app: App,

/// The purpose of this parsed file.
#[serde(default = "Metadata::default_purpose")]
purpose: Purpose,

/// The schema version for the configuration.
#[serde(default = "Metadata::default_schema_version")]
#[serde(flatten)]
version: Version,
schema_version: Version,
}

impl Default for Metadata {
fn default() -> Self {
Self {
version: Self::default_version(),
app: Self::default_app(),
purpose: Self::default_purpose(),
schema_version: Self::default_schema_version(),
}
}
}

impl Metadata {
fn default_version() -> Version {
fn default_app() -> App {
App::TorrustIndex
}

fn default_purpose() -> Purpose {
Purpose::Configuration
}

fn default_schema_version() -> Version {
Version::latest()
}
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display, Clone)]
#[serde(rename_all = "kebab-case")]
pub enum App {
TorrustIndex,
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display, Clone)]
#[serde(rename_all = "lowercase")]
pub enum Purpose {
Configuration,
}

/// The configuration version.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display, Clone)]
#[serde(rename_all = "lowercase")]
pub struct Version {
#[serde(default = "Version::default_semver")]
version: String,
schema_version: String,
}

impl Default for Version {
fn default() -> Self {
Self {
version: Self::default_semver(),
schema_version: Self::default_semver(),
}
}
}

impl Version {
fn new(semver: &str) -> Self {
Self {
version: semver.to_owned(),
schema_version: semver.to_owned(),
}
}

fn latest() -> Self {
Self {
version: LATEST_VERSION.to_string(),
schema_version: LATEST_VERSION.to_string(),
}
}

Expand Down Expand Up @@ -284,9 +317,9 @@ impl Configuration {

let settings: Settings = figment.extract()?;

if settings.metadata.version != Version::new(VERSION_2) {
if settings.metadata.schema_version != Version::new(VERSION_2) {
return Err(Error::UnsupportedVersion {
version: settings.metadata.version,
version: settings.metadata.schema_version,
});
}

Expand Down Expand Up @@ -320,7 +353,10 @@ mod tests {

#[cfg(test)]
fn default_config_toml() -> String {
let config = r#"version = "2"
let config = r#"[metadata]
app = "torrust-index"
purpose = "configuration"
schema_version = "2.0.0"
[logging]
threshold = "info"
Expand Down
1 change: 0 additions & 1 deletion src/config/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use super::Metadata;
pub struct Settings {
/// Configuration metadata.
#[serde(default = "Settings::default_metadata")]
#[serde(flatten)]
pub metadata: Metadata,

/// The logging configuration.
Expand Down
6 changes: 5 additions & 1 deletion src/web/api/server/v1/contexts/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
//! ```json
//! {
//! "data": {
//! "version": "2",
//! "metadata": {
//! "app": "torrust-index",
//! "purpose": "configuration",
//! "schema_version": "2.0.0"
//! },
//! "logging": {
//! "threshold": "info"
//! },
Expand Down

0 comments on commit 5bd611b

Please sign in to comment.