diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index 15b8b67f..1bbea804 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The Matrix.org Foundation C.I.C. +// Copyright 2021, 2022 The Matrix.org Foundation C.I.C. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -22,99 +22,8 @@ //! Application configuration logic -use async_trait::async_trait; -use schemars::JsonSchema; -use serde::{Deserialize, Serialize}; - -mod cookies; -mod csrf; -mod database; -mod email; -mod http; -mod oauth2; pub(crate) mod schema; -mod telemetry; -mod templates; -mod util; +mod sections; +pub(crate) mod util; -pub use self::{ - cookies::CookiesConfig, - csrf::CsrfConfig, - database::DatabaseConfig, - email::{EmailConfig, EmailSmtpMode, EmailTransportConfig}, - http::HttpConfig, - oauth2::{OAuth2ClientAuthMethodConfig, OAuth2ClientConfig, OAuth2Config}, - telemetry::{ - MetricsConfig, MetricsExporterConfig, Propagator, TelemetryConfig, TracingConfig, - TracingExporterConfig, - }, - templates::TemplatesConfig, - util::ConfigurationSection, -}; - -/// Application configuration root -#[derive(Debug, Serialize, Deserialize, JsonSchema)] -pub struct RootConfig { - /// Configuration related to OAuth 2.0/OIDC operations - pub oauth2: OAuth2Config, - - /// Configuration of the HTTP server - #[serde(default)] - pub http: HttpConfig, - - /// Database connection configuration - #[serde(default)] - pub database: DatabaseConfig, - - /// Configuration related to cookies - pub cookies: CookiesConfig, - - /// Configuration related to sending monitoring data - #[serde(default)] - pub telemetry: TelemetryConfig, - - /// Configuration related to templates - #[serde(default)] - pub templates: TemplatesConfig, - - /// Configuration related to Cross-Site Request Forgery protections - #[serde(default)] - pub csrf: CsrfConfig, - - /// Configuration related to sending emails - #[serde(default)] - pub email: EmailConfig, -} - -#[async_trait] -impl ConfigurationSection<'_> for RootConfig { - fn path() -> &'static str { - "" - } - - async fn generate() -> anyhow::Result { - Ok(Self { - oauth2: OAuth2Config::generate().await?, - http: HttpConfig::generate().await?, - database: DatabaseConfig::generate().await?, - cookies: CookiesConfig::generate().await?, - telemetry: TelemetryConfig::generate().await?, - templates: TemplatesConfig::generate().await?, - csrf: CsrfConfig::generate().await?, - email: EmailConfig::generate().await?, - }) - } - - fn test() -> Self { - Self { - oauth2: OAuth2Config::test(), - http: HttpConfig::test(), - database: DatabaseConfig::test(), - cookies: CookiesConfig::test(), - telemetry: TelemetryConfig::test(), - templates: TemplatesConfig::test(), - csrf: CsrfConfig::test(), - email: EmailConfig::test(), - } - } -} +pub use self::{sections::*, util::ConfigurationSection}; diff --git a/crates/config/src/cookies.rs b/crates/config/src/sections/cookies.rs similarity index 96% rename from crates/config/src/cookies.rs rename to crates/config/src/sections/cookies.rs index 560d2cc7..daca0c5f 100644 --- a/crates/config/src/cookies.rs +++ b/crates/config/src/sections/cookies.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The Matrix.org Foundation C.I.C. +// Copyright 2021, 2022 The Matrix.org Foundation C.I.C. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/crates/config/src/csrf.rs b/crates/config/src/sections/csrf.rs similarity index 97% rename from crates/config/src/csrf.rs rename to crates/config/src/sections/csrf.rs index 714085b0..ee74d4d9 100644 --- a/crates/config/src/csrf.rs +++ b/crates/config/src/sections/csrf.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The Matrix.org Foundation C.I.C. +// Copyright 2021, 2022 The Matrix.org Foundation C.I.C. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/crates/config/src/database.rs b/crates/config/src/sections/database.rs similarity index 99% rename from crates/config/src/database.rs rename to crates/config/src/sections/database.rs index 87b5441c..69cfb451 100644 --- a/crates/config/src/database.rs +++ b/crates/config/src/sections/database.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The Matrix.org Foundation C.I.C. +// Copyright 2021, 2022 The Matrix.org Foundation C.I.C. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/crates/config/src/email.rs b/crates/config/src/sections/email.rs similarity index 100% rename from crates/config/src/email.rs rename to crates/config/src/sections/email.rs diff --git a/crates/config/src/http.rs b/crates/config/src/sections/http.rs similarity index 97% rename from crates/config/src/http.rs rename to crates/config/src/sections/http.rs index 9dd1850c..c1a5d4bb 100644 --- a/crates/config/src/http.rs +++ b/crates/config/src/sections/http.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The Matrix.org Foundation C.I.C. +// Copyright 2021, 2022 The Matrix.org Foundation C.I.C. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/crates/config/src/sections/mod.rs b/crates/config/src/sections/mod.rs new file mode 100644 index 00000000..4936b290 --- /dev/null +++ b/crates/config/src/sections/mod.rs @@ -0,0 +1,108 @@ +// Copyright 2022 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use async_trait::async_trait; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +mod cookies; +mod csrf; +mod database; +mod email; +mod http; +mod oauth2; +mod telemetry; +mod templates; + +pub use self::{ + cookies::CookiesConfig, + csrf::CsrfConfig, + database::DatabaseConfig, + email::{EmailConfig, EmailSmtpMode, EmailTransportConfig}, + http::HttpConfig, + oauth2::{OAuth2ClientAuthMethodConfig, OAuth2ClientConfig, OAuth2Config}, + telemetry::{ + MetricsConfig, MetricsExporterConfig, Propagator, TelemetryConfig, TracingConfig, + TracingExporterConfig, + }, + templates::TemplatesConfig, +}; +use crate::util::ConfigurationSection; + +/// Application configuration root +#[derive(Debug, Serialize, Deserialize, JsonSchema)] +pub struct RootConfig { + /// Configuration related to OAuth 2.0/OIDC operations + pub oauth2: OAuth2Config, + + /// Configuration of the HTTP server + #[serde(default)] + pub http: HttpConfig, + + /// Database connection configuration + #[serde(default)] + pub database: DatabaseConfig, + + /// Configuration related to cookies + pub cookies: CookiesConfig, + + /// Configuration related to sending monitoring data + #[serde(default)] + pub telemetry: TelemetryConfig, + + /// Configuration related to templates + #[serde(default)] + pub templates: TemplatesConfig, + + /// Configuration related to Cross-Site Request Forgery protections + #[serde(default)] + pub csrf: CsrfConfig, + + /// Configuration related to sending emails + #[serde(default)] + pub email: EmailConfig, +} + +#[async_trait] +impl ConfigurationSection<'_> for RootConfig { + fn path() -> &'static str { + "" + } + + async fn generate() -> anyhow::Result { + Ok(Self { + oauth2: OAuth2Config::generate().await?, + http: HttpConfig::generate().await?, + database: DatabaseConfig::generate().await?, + cookies: CookiesConfig::generate().await?, + telemetry: TelemetryConfig::generate().await?, + templates: TemplatesConfig::generate().await?, + csrf: CsrfConfig::generate().await?, + email: EmailConfig::generate().await?, + }) + } + + fn test() -> Self { + Self { + oauth2: OAuth2Config::test(), + http: HttpConfig::test(), + database: DatabaseConfig::test(), + cookies: CookiesConfig::test(), + telemetry: TelemetryConfig::test(), + templates: TemplatesConfig::test(), + csrf: CsrfConfig::test(), + email: EmailConfig::test(), + } + } +} diff --git a/crates/config/src/oauth2.rs b/crates/config/src/sections/oauth2.rs similarity index 99% rename from crates/config/src/oauth2.rs rename to crates/config/src/sections/oauth2.rs index cdd313cd..05fcb290 100644 --- a/crates/config/src/oauth2.rs +++ b/crates/config/src/sections/oauth2.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The Matrix.org Foundation C.I.C. +// Copyright 2021, 2022 The Matrix.org Foundation C.I.C. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/crates/config/src/telemetry.rs b/crates/config/src/sections/telemetry.rs similarity index 98% rename from crates/config/src/telemetry.rs rename to crates/config/src/sections/telemetry.rs index 3475ff23..53be4365 100644 --- a/crates/config/src/telemetry.rs +++ b/crates/config/src/sections/telemetry.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The Matrix.org Foundation C.I.C. +// Copyright 2021, 2022 The Matrix.org Foundation C.I.C. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/crates/config/src/templates.rs b/crates/config/src/sections/templates.rs similarity index 96% rename from crates/config/src/templates.rs rename to crates/config/src/sections/templates.rs index 1dc00018..a3273425 100644 --- a/crates/config/src/templates.rs +++ b/crates/config/src/sections/templates.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The Matrix.org Foundation C.I.C. +// Copyright 2021, 2022 The Matrix.org Foundation C.I.C. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License.