1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-07-29 22:01:14 +03:00

Templates config

This commit is contained in:
Quentin Gliech
2021-09-23 23:51:17 +02:00
parent 7c58bb4c17
commit ff41ae5762
5 changed files with 32 additions and 6 deletions

View File

@ -68,8 +68,8 @@ impl ServerCommand {
queue.start(); queue.start();
// Load and compile the templates // Load and compile the templates
// TODO: custom template path from the config let templates =
let templates = Templates::load(None, true).context("could not load templates")?; Templates::load_from_config(&config.templates).context("could not load templates")?;
// Start the server // Start the server
let root = mas_core::handlers::root(&pool, &templates, &config); let root = mas_core::handlers::root(&pool, &templates, &config);

View File

@ -41,6 +41,10 @@ enum TemplatesSubcommand {
Check { Check {
/// Path where the templates are /// Path where the templates are
path: String, path: String,
/// Skip loading builtin templates
#[clap(long)]
skip_builtin: bool,
}, },
} }
@ -54,8 +58,8 @@ impl TemplatesCommand {
Ok(()) Ok(())
} }
SC::Check { path } => { SC::Check { path, skip_builtin } => {
Templates::load(Some(path.clone()), false)?; Templates::load(Some(path), !skip_builtin)?;
Ok(()) Ok(())
} }

View File

@ -21,6 +21,7 @@ mod csrf;
mod database; mod database;
mod http; mod http;
mod oauth2; mod oauth2;
mod templates;
mod util; mod util;
pub use self::{ pub use self::{
@ -29,6 +30,7 @@ pub use self::{
database::DatabaseConfig, database::DatabaseConfig,
http::HttpConfig, http::HttpConfig,
oauth2::{Algorithm, KeySet, OAuth2ClientConfig, OAuth2Config}, oauth2::{Algorithm, KeySet, OAuth2ClientConfig, OAuth2Config},
templates::TemplatesConfig,
util::ConfigurationSection, util::ConfigurationSection,
}; };
@ -44,6 +46,9 @@ pub struct RootConfig {
pub cookies: CookiesConfig, pub cookies: CookiesConfig,
#[serde(default)]
pub templates: TemplatesConfig,
#[serde(default)] #[serde(default)]
pub csrf: CsrfConfig, pub csrf: CsrfConfig,
} }
@ -60,6 +65,7 @@ impl ConfigurationSection<'_> for RootConfig {
http: HttpConfig::generate().await?, http: HttpConfig::generate().await?,
database: DatabaseConfig::generate().await?, database: DatabaseConfig::generate().await?,
cookies: CookiesConfig::generate().await?, cookies: CookiesConfig::generate().await?,
templates: TemplatesConfig::generate().await?,
csrf: CsrfConfig::generate().await?, csrf: CsrfConfig::generate().await?,
}) })
} }
@ -70,6 +76,7 @@ impl ConfigurationSection<'_> for RootConfig {
http: HttpConfig::test(), http: HttpConfig::test(),
database: DatabaseConfig::test(), database: DatabaseConfig::test(),
cookies: CookiesConfig::test(), cookies: CookiesConfig::test(),
templates: TemplatesConfig::test(),
csrf: CsrfConfig::test(), csrf: CsrfConfig::test(),
} }
} }

View File

@ -90,12 +90,21 @@ struct FieldError<FieldType> {
error: Box<dyn HtmlError>, error: Box<dyn HtmlError>,
} }
#[derive(Debug, Default)] #[derive(Debug)]
pub struct ErroredForm<FieldType> { pub struct ErroredForm<FieldType> {
form: Vec<FormError>, form: Vec<FormError>,
fields: Vec<FieldError<FieldType>>, fields: Vec<FieldError<FieldType>>,
} }
impl<T> Default for ErroredForm<T> {
fn default() -> Self {
Self {
form: Vec::new(),
fields: Vec::new(),
}
}
}
impl<T> ErroredForm<T> { impl<T> ErroredForm<T> {
#[must_use] #[must_use]
pub fn new() -> Self { pub fn new() -> Self {

View File

@ -17,6 +17,7 @@
use std::{collections::HashSet, io::Cursor, path::Path, string::ToString, sync::Arc}; use std::{collections::HashSet, io::Cursor, path::Path, string::ToString, sync::Arc};
use anyhow::Context as _; use anyhow::Context as _;
use mas_config::TemplatesConfig;
use serde::Serialize; use serde::Serialize;
use tera::{Context, Error as TeraError, Tera}; use tera::{Context, Error as TeraError, Tera};
use thiserror::Error; use thiserror::Error;
@ -50,13 +51,18 @@ pub enum TemplateLoadingError {
} }
impl Templates { impl Templates {
/// Load the templates from [the config][`TemplatesConfig`]
pub fn load_from_config(config: &TemplatesConfig) -> Result<Self, TemplateLoadingError> {
Self::load(config.path.as_deref(), config.builtin)
}
/// Load the templates and check all needed templates are properly loaded /// Load the templates and check all needed templates are properly loaded
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `path` - An optional path to where templates should be loaded /// * `path` - An optional path to where templates should be loaded
/// * `builtin` - Set to `true` to load the builtin templates as well /// * `builtin` - Set to `true` to load the builtin templates as well
pub fn load(path: Option<String>, builtin: bool) -> Result<Self, TemplateLoadingError> { pub fn load(path: Option<&str>, builtin: bool) -> Result<Self, TemplateLoadingError> {
let tera = { let tera = {
let mut tera = Tera::default(); let mut tera = Tera::default();