From bd5604be63e8fd9cc29da34028f55c3f0dddaf99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristan=20Dani=C3=ABl=20Maat?= Date: Sat, 11 Jul 2020 22:20:32 +0100 Subject: [PATCH] Read StaticFiles from the templates directory This may seem a bit paradoxical, but since the templates are generated using parcel, and this just dumps everything into the root, static files are kept in the same directory as template files. This may change in the future. --- src/main.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 41eb0f1..9161325 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ #![feature(proc_macro_hygiene, decl_macro)] +use rocket::fairing::AdHoc; + use rocket_contrib::serve::StaticFiles; use rocket_contrib::templates::Template; @@ -15,6 +17,18 @@ fn main() { .attach(Template::fairing()) .mount("/", mail_routes()) .mount("/", static_templates()) - .mount("/", StaticFiles::from("templates")) + .attach(AdHoc::on_attach("Static files config", |rocket| { + let static_path = match rocket.config().get_string("template_dir") { + Ok(dir) => dir, + Err(rocket::config::ConfigError::Missing { .. }) => "templates".to_string(), + Err(err) => { + eprintln!("Error reading configuration: {}", err); + eprintln!("Using default templates path."); + "templates".to_string() + }, + }; + + Ok(rocket.mount("/", StaticFiles::from(static_path))) + })) .launch(); }