From ff41ae57624837240805f6492d78d5b6d3e002a3 Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Thu, 23 Sep 2021 23:51:17 +0200 Subject: [PATCH] Templates config --- crates/cli/src/server.rs | 4 ++-- crates/cli/src/templates.rs | 8 ++++++-- crates/config/src/lib.rs | 7 +++++++ crates/core/src/errors.rs | 11 ++++++++++- crates/core/src/templates/mod.rs | 8 +++++++- 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/crates/cli/src/server.rs b/crates/cli/src/server.rs index fe6cb412..2edcb4c4 100644 --- a/crates/cli/src/server.rs +++ b/crates/cli/src/server.rs @@ -68,8 +68,8 @@ impl ServerCommand { queue.start(); // Load and compile the templates - // TODO: custom template path from the config - let templates = Templates::load(None, true).context("could not load templates")?; + let templates = + Templates::load_from_config(&config.templates).context("could not load templates")?; // Start the server let root = mas_core::handlers::root(&pool, &templates, &config); diff --git a/crates/cli/src/templates.rs b/crates/cli/src/templates.rs index c9a3e06e..1b9f67d8 100644 --- a/crates/cli/src/templates.rs +++ b/crates/cli/src/templates.rs @@ -41,6 +41,10 @@ enum TemplatesSubcommand { Check { /// Path where the templates are path: String, + + /// Skip loading builtin templates + #[clap(long)] + skip_builtin: bool, }, } @@ -54,8 +58,8 @@ impl TemplatesCommand { Ok(()) } - SC::Check { path } => { - Templates::load(Some(path.clone()), false)?; + SC::Check { path, skip_builtin } => { + Templates::load(Some(path), !skip_builtin)?; Ok(()) } diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index c06de274..8a6631be 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -21,6 +21,7 @@ mod csrf; mod database; mod http; mod oauth2; +mod templates; mod util; pub use self::{ @@ -29,6 +30,7 @@ pub use self::{ database::DatabaseConfig, http::HttpConfig, oauth2::{Algorithm, KeySet, OAuth2ClientConfig, OAuth2Config}, + templates::TemplatesConfig, util::ConfigurationSection, }; @@ -44,6 +46,9 @@ pub struct RootConfig { pub cookies: CookiesConfig, + #[serde(default)] + pub templates: TemplatesConfig, + #[serde(default)] pub csrf: CsrfConfig, } @@ -60,6 +65,7 @@ impl ConfigurationSection<'_> for RootConfig { http: HttpConfig::generate().await?, database: DatabaseConfig::generate().await?, cookies: CookiesConfig::generate().await?, + templates: TemplatesConfig::generate().await?, csrf: CsrfConfig::generate().await?, }) } @@ -70,6 +76,7 @@ impl ConfigurationSection<'_> for RootConfig { http: HttpConfig::test(), database: DatabaseConfig::test(), cookies: CookiesConfig::test(), + templates: TemplatesConfig::test(), csrf: CsrfConfig::test(), } } diff --git a/crates/core/src/errors.rs b/crates/core/src/errors.rs index d7cb5a9c..44180222 100644 --- a/crates/core/src/errors.rs +++ b/crates/core/src/errors.rs @@ -90,12 +90,21 @@ struct FieldError { error: Box, } -#[derive(Debug, Default)] +#[derive(Debug)] pub struct ErroredForm { form: Vec, fields: Vec>, } +impl Default for ErroredForm { + fn default() -> Self { + Self { + form: Vec::new(), + fields: Vec::new(), + } + } +} + impl ErroredForm { #[must_use] pub fn new() -> Self { diff --git a/crates/core/src/templates/mod.rs b/crates/core/src/templates/mod.rs index 678893ed..48dfbe7d 100644 --- a/crates/core/src/templates/mod.rs +++ b/crates/core/src/templates/mod.rs @@ -17,6 +17,7 @@ use std::{collections::HashSet, io::Cursor, path::Path, string::ToString, sync::Arc}; use anyhow::Context as _; +use mas_config::TemplatesConfig; use serde::Serialize; use tera::{Context, Error as TeraError, Tera}; use thiserror::Error; @@ -50,13 +51,18 @@ pub enum TemplateLoadingError { } impl Templates { + /// Load the templates from [the config][`TemplatesConfig`] + pub fn load_from_config(config: &TemplatesConfig) -> Result { + Self::load(config.path.as_deref(), config.builtin) + } + /// Load the templates and check all needed templates are properly loaded /// /// # Arguments /// /// * `path` - An optional path to where templates should be loaded /// * `builtin` - Set to `true` to load the builtin templates as well - pub fn load(path: Option, builtin: bool) -> Result { + pub fn load(path: Option<&str>, builtin: bool) -> Result { let tera = { let mut tera = Tera::default();