diff --git a/examples/demo/src/initializers/view_engine.rs b/examples/demo/src/initializers/view_engine.rs index f0a5ffba9..8a751d1af 100644 --- a/examples/demo/src/initializers/view_engine.rs +++ b/examples/demo/src/initializers/view_engine.rs @@ -18,6 +18,7 @@ impl Initializer for ViewEngineInitializer { } async fn after_routes(&self, router: AxumRouter, _ctx: &AppContext) -> Result { + #[allow(unused_mut)] let mut tera_engine = engines::TeraView::build()?; if std::path::Path::new(I18N_DIR).exists() { let arc = ArcLoader::builder(&I18N_DIR, unic_langid::langid!("en-US")) @@ -25,6 +26,14 @@ impl Initializer for ViewEngineInitializer { .customize(|bundle| bundle.set_use_isolating(false)) .build() .map_err(|e| Error::string(&e.to_string()))?; + #[cfg(debug_assertions)] + tera_engine + .tera + .lock() + .expect("lock") + .register_function("t", FluentLoader::new(arc)); + + #[cfg(not(debug_assertions))] tera_engine .tera .register_function("t", FluentLoader::new(arc)); diff --git a/src/controller/middleware/cors.rs b/src/controller/middleware/cors.rs index c821fc8fe..0aa0232d2 100644 --- a/src/controller/middleware/cors.rs +++ b/src/controller/middleware/cors.rs @@ -45,7 +45,7 @@ impl Default for Cors { } fn default_allow_origins() -> Vec { - vec!["*".to_string()] + vec!["any".to_string()] } fn default_allow_headers() -> Vec { diff --git a/src/controller/views/engines.rs b/src/controller/views/engines.rs index 9e924703a..3159f7e91 100644 --- a/src/controller/views/engines.rs +++ b/src/controller/views/engines.rs @@ -8,7 +8,15 @@ const VIEWS_DIR: &str = "assets/views"; #[derive(Clone, Debug)] pub struct TeraView { + #[cfg(debug_assertions)] + pub tera: std::sync::Arc>, + + #[cfg(not(debug_assertions))] pub tera: tera::Tera, + + #[cfg(debug_assertions)] + pub view_dir: String, + pub default_context: tera::Context, } @@ -44,7 +52,12 @@ impl TeraView { )?; let ctx = tera::Context::default(); Ok(Self { - tera, + #[cfg(debug_assertions)] + view_dir: path.as_ref().to_string_lossy().to_string(), + #[cfg(debug_assertions)] + tera: std::sync::Arc::new(std::sync::Mutex::new(tera)), + #[cfg(not(debug_assertions))] + tera: tera, default_context: ctx, }) } @@ -52,30 +65,22 @@ impl TeraView { impl ViewRenderer for TeraView { fn render(&self, key: &str, data: S) -> Result { + #[cfg(debug_assertions)] + use std::borrow::BorrowMut; + let context = tera::Context::from_serialize(data)?; - // NOTE: this supports full reload of template for every render request. - // it means that you will see refreshed content without rebuild and rerun - // of the app. - // the code here is required, since Tera has no "build every time your render" - // mode, which would have been better. - // we minimize risk by flagging this in debug (development) builds only - // for now we leave this commented out, we propose people use `cargo-watch` - // we want to delay using un__safe as much as possible. - /* #[cfg(debug_assertions)] - { - let ptr = std::ptr::addr_of!(self.tera); - let mut_ptr = ptr.cast_mut(); - // fix this keyword - un__safe { - let tera = &mut *mut_ptr; - tera.full_reload()?; - } - } - */ + tracing::debug!(key = key, "Tera rendering in non-optimized debug mode"); + #[cfg(debug_assertions)] + return Ok(self.tera.lock().expect("lock").borrow_mut().render_str( + &std::fs::read_to_string(Path::new(&self.view_dir).join(key)) + .map_err(|_e| tera::Error::template_not_found(key))?, + &context, + )?); - Ok(self.tera.render(key, &context)?) + #[cfg(not(debug_assertions))] + return Ok(self.tera.render(key, &context)?); } } diff --git a/starters/saas/Cargo.lock b/starters/saas/Cargo.lock index 03d228410..bd0ab52a1 100644 --- a/starters/saas/Cargo.lock +++ b/starters/saas/Cargo.lock @@ -2266,6 +2266,8 @@ dependencies = [ [[package]] name = "loco-gen" version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1815e1bd51c41fceb096b27cf98b52ab23d272025337ef5106e549e697e8ee9f" dependencies = [ "chrono", "clap", @@ -2283,6 +2285,8 @@ dependencies = [ [[package]] name = "loco-rs" version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4aa14998bbea030a703e8017a5a86c368b022c74b6848f5c16f07adfbc3b98b3" dependencies = [ "argon2", "async-trait",