From fc0b9d7fa5f73e47eccf63f4a73ff6efdbcb63c3 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 16 Apr 2018 00:24:55 +0200 Subject: 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. --- src/main.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 30b371eaed..a16ee2c0d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,9 +20,6 @@ extern crate diesel; #[macro_use] extern crate log; -#[macro_use] -extern crate tera; - #[macro_use] extern crate serde_derive; @@ -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 { fn start_renderer() -> Addr { 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, -- cgit 1.4.1