From 81681303b2ca21c75aed030863399bf70f80e635 Mon Sep 17 00:00:00 2001
From: tuxmain <tuxmain@zettascript.org>
Date: Wed, 26 Oct 2022 16:03:22 +0200
Subject: [PATCH] Config: templates dir

---
 src/config.rs    | 7 +++++++
 src/main.rs      | 8 +++-----
 src/templates.rs | 8 ++++----
 3 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/src/config.rs b/src/config.rs
index 9b4fa6f..d93392e 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -60,6 +60,9 @@ pub struct Config {
 	pub reverse_proxy: bool,
 	#[serde(default = "Config::default_root_url")]
 	pub root_url: String,
+	/// Templates directory. May be absolute or relative to config/data directory.
+	#[serde(default = "Config::default_templates_dir")]
+	pub templates_dir: String,
 }
 
 impl Config {
@@ -129,6 +132,9 @@ impl Config {
 	fn default_root_url() -> String {
 		"/".into()
 	}
+	fn default_templates_dir() -> String {
+		"templates".into()
+	}
 }
 
 impl Default for Config {
@@ -156,6 +162,7 @@ impl Default for Config {
 			matrix_user: Self::default_matrix_user(),
 			reverse_proxy: Self::default_reverse_proxy(),
 			root_url: Self::default_root_url(),
+			templates_dir: Self::default_templates_dir(),
 		}
 	}
 }
diff --git a/src/main.rs b/src/main.rs
index c351c99..68171f4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -50,11 +50,9 @@ fn init_all(
 	subopt: cli::StartOpt,
 ) -> (config::Config, db::Dbs, templates::Templates) {
 	std::fs::create_dir_all(&opt.dir.0).expect("Cannot create dir");
+	let config = config::read_config(&opt.dir.0);
 	let dbs = db::load_dbs((!subopt.tmp).then_some(&opt.dir.0));
+	let templates = templates::Templates::new(&opt.dir.0, &config);
 
-	(
-		config::read_config(&opt.dir.0),
-		dbs,
-		templates::Templates::new(&opt.dir.0),
-	)
+	(config, dbs, templates)
 }
diff --git a/src/templates.rs b/src/templates.rs
index bb9e827..dc9fb5b 100644
--- a/src/templates.rs
+++ b/src/templates.rs
@@ -1,4 +1,4 @@
-use crate::db::*;
+use crate::{config::Config, db::*};
 
 use serde::Serialize;
 use std::path::Path;
@@ -17,15 +17,15 @@ pub struct Templates {
 }
 
 impl Templates {
-	pub fn new(dir: &Path) -> Self {
-		let dir = dir.join("templates");
+	pub fn new(dir: &Path, config: &Config) -> Self {
+		let dir = dir.join(&config.templates_dir);
 		std::fs::create_dir_all(&dir).expect("Cannot create templates dir");
 
 		for &(file, default) in TEMPLATE_FILES {
 			let file_path = dir.join(file);
 			if !file_path.is_file() {
 				std::fs::write(file_path, default)
-					.unwrap_or_else(|_| panic!("Cannot write template file {}", file));
+					.unwrap_or_else(|_| panic!("Cannot write template file {file}"));
 			}
 		}