diff options
author | Vincent Ambo <tazjin@gmail.com> | 2018-04-15T22·24+0200 |
---|---|---|
committer | Vincent Ambo <tazjin@gmail.com> | 2018-04-15T22·24+0200 |
commit | fc0b9d7fa5f73e47eccf63f4a73ff6efdbcb63c3 (patch) | |
tree | ad91d8a3f222411e62431961216de8148df73b2e /src | |
parent | 71c55b8bb8059654049489d20a84f53d38ff1666 (diff) |
refactor(main): Include Tera templates into application binary
Instead of loading the templates at launch time (which requires the template folder to be present), include the template strings into the binary. This also re-enables auto-escaping in Tera.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs index 30b371eaede5..a16ee2c0d9a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,9 +21,6 @@ extern crate diesel; extern crate log; #[macro_use] -extern crate tera; - -#[macro_use] extern crate serde_derive; #[macro_use] @@ -42,6 +39,7 @@ extern crate rand; extern crate reqwest; extern crate serde; extern crate serde_json; +extern crate tera; extern crate tokio; extern crate tokio_timer; extern crate url; @@ -75,8 +73,9 @@ use diesel::r2d2::{ConnectionManager, Pool}; use handlers::*; use oidc::OidcExecutor; use rand::{OsRng, Rng}; -use std::env; use render::Renderer; +use std::env; +use tera::Tera; fn config(name: &str) -> String { env::var(name).expect(&format!("{} must be set", name)) @@ -128,9 +127,18 @@ fn start_oidc_executor(base_url: &str) -> Addr<Syn, OidcExecutor> { fn start_renderer() -> Addr<Syn, Renderer> { info!("Compiling templates ..."); - let template_path = concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"); - let mut tera = compile_templates!(template_path); - tera.autoescape_on(vec![]); + let mut tera: Tera = Default::default(); + + // Include template strings into the binary instead of being + // location-dependent. + // Drawback is that template changes require recompilation ... + tera.add_raw_templates(vec![ + ("index.html", include_str!("../templates/index.html")), + ("post.html", include_str!("../templates/post.html")), + ("search.html", include_str!("../templates/search.html")), + ("thread.html", include_str!("../templates/thread.html")), + ]).expect("Could not compile templates"); + let comrak = comrak::ComrakOptions{ github_pre_lang: true, ext_strikethrough: true, |