diff --git a/Cargo.lock b/Cargo.lock index fea39b56..3f6cb777 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -984,7 +984,7 @@ dependencies = [ "fast_chemail", "fnv", "futures-util", - "handlebars", + "handlebars 4.5.0", "http 0.2.9", "indexmap 2.1.0", "mime", @@ -3168,24 +3168,24 @@ dependencies = [ [[package]] name = "fluent-template-macros" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dec7592cd1f45c1afe9084ce59c62a3a7c266c125c4c2ec97e95b0563c4aa914" +checksum = "0c58fd7421bad2b89506827409317a3088b74d0d637202003f2e87efdc43ae8e" dependencies = [ "flume 0.10.14", "ignore", "once_cell", "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.48", "unic-langid", ] [[package]] name = "fluent-templates" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c3ef2c2152757885365abce32ddf682746062f1b6b3c0824a29fbed6ee4d080" +checksum = "fc023356542b155925aa5e433806ddd33acb46f0218541f869b342676332cd79" dependencies = [ "arc-swap", "fluent", @@ -3194,7 +3194,7 @@ dependencies = [ "fluent-syntax", "fluent-template-macros", "flume 0.10.14", - "handlebars", + "handlebars 5.1.0", "heck", "ignore", "intl-memoizer", @@ -3678,6 +3678,20 @@ name = "handlebars" version = "4.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "faa67bab9ff362228eb3d00bd024a4965d8231bbb7921167f0cfa66c6626b225" +dependencies = [ + "log", + "pest", + "pest_derive", + "serde", + "serde_json", + "thiserror", +] + +[[package]] +name = "handlebars" +version = "5.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab283476b99e66691dee3f1640fea91487a8d81f50fb5ecc75538f8f8879a1e4" dependencies = [ "log", "pest", @@ -7600,8 +7614,9 @@ version = "1.0.0" dependencies = [ "actix-web", "actix-web-lab", + "env_logger", "fluent-templates", - "handlebars", + "handlebars 5.1.0", "serde", "serde_json", ] @@ -7611,7 +7626,9 @@ name = "templating-handlebars" version = "1.0.0" dependencies = [ "actix-web", - "handlebars", + "actix-web-lab", + "env_logger", + "handlebars 5.1.0", "serde_json", ] diff --git a/templating/fluent/Cargo.toml b/templating/fluent/Cargo.toml index f6ed7ce4..53b29e03 100644 --- a/templating/fluent/Cargo.toml +++ b/templating/fluent/Cargo.toml @@ -6,7 +6,8 @@ edition = "2021" [dependencies] actix-web.workspace = true actix-web-lab.workspace = true -fluent-templates = { version = "0.8", features = ["handlebars"] } -handlebars = { version = "4.5", features = ["dir_source"] } +env_logger.workspace = true +fluent-templates = { version = "0.9", features = ["handlebars"] } +handlebars = { version = "5", features = ["dir_source"] } serde.workspace = true serde_json.workspace = true diff --git a/templating/fluent/src/main.rs b/templating/fluent/src/main.rs index 8732a35e..d8c6cde8 100644 --- a/templating/fluent/src/main.rs +++ b/templating/fluent/src/main.rs @@ -10,7 +10,7 @@ use actix_web::{ }; use actix_web_lab::{extract::Path, respond::Html}; use fluent_templates::{static_loader, FluentLoader, Loader as _}; -use handlebars::Handlebars; +use handlebars::{DirectorySourceOptions, Handlebars}; use serde_json::json; mod lang_choice; @@ -51,13 +51,22 @@ async fn user( #[actix_web::main] async fn main() -> io::Result<()> { + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + // Handlebars uses a repository for the compiled templates. This object must be shared between // the application threads, and is therefore passed to the App in an Arc. let mut handlebars = Handlebars::new(); // register template dir with Handlebars registry handlebars - .register_templates_directory(".html", "./templates") + .register_templates_directory( + "./templates", + DirectorySourceOptions { + tpl_extension: ".html".to_owned(), + hidden: false, + temporary: false, + }, + ) .unwrap(); // register Fluent helper with Handlebars registry @@ -72,6 +81,7 @@ async fn main() -> io::Result<()> { .service(index) .service(user) }) + .workers(2) .bind(("127.0.0.1", 8080))? .run() .await @@ -85,7 +95,7 @@ fn error_handlers() -> ErrorHandlers { // Error handler for a 404 Page not found error. fn not_found(res: ServiceResponse) -> Result> { let lang = LangChoice::from_req(res.request()).lang_id(); - let error = LOCALES.lookup(&lang, "error-not-found").unwrap(); + let error = LOCALES.lookup(&lang, "error-not-found"); let response = get_error_response(&res, &error); diff --git a/templating/handlebars/Cargo.toml b/templating/handlebars/Cargo.toml index 374e29c0..4079d49f 100644 --- a/templating/handlebars/Cargo.toml +++ b/templating/handlebars/Cargo.toml @@ -5,5 +5,7 @@ edition = "2021" [dependencies] actix-web.workspace = true -handlebars = { version = "4.5", features = ["dir_source"] } +actix-web-lab.workspace = true +env_logger.workspace = true +handlebars = { version = "5", features = ["dir_source"] } serde_json.workspace = true diff --git a/templating/handlebars/src/main.rs b/templating/handlebars/src/main.rs index 3e4bb1b6..ba364217 100644 --- a/templating/handlebars/src/main.rs +++ b/templating/handlebars/src/main.rs @@ -6,24 +6,24 @@ use actix_web::{ get, http::{header::ContentType, StatusCode}, middleware::{ErrorHandlerResponse, ErrorHandlers}, - web, App, HttpResponse, HttpServer, Result, + web, App, HttpResponse, HttpServer, Responder, Result, }; -use handlebars::Handlebars; +use actix_web_lab::respond::Html; +use handlebars::{DirectorySourceOptions, Handlebars}; use serde_json::json; -// Macro documentation can be found in the actix_web_codegen crate #[get("/")] -async fn index(hb: web::Data>) -> HttpResponse { +async fn index(hb: web::Data>) -> impl Responder { let data = json!({ "name": "Handlebars" }); let body = hb.render("index", &data).unwrap(); - HttpResponse::Ok().body(body) + Html(body) } #[get("/{user}/{data}")] -async fn user(hb: web::Data>, path: web::Path<(String, String)>) -> HttpResponse { +async fn user(hb: web::Data>, path: web::Path<(String, String)>) -> impl Responder { let info = path.into_inner(); let data = json!({ "user": info.0, @@ -31,17 +31,26 @@ async fn user(hb: web::Data>, path: web::Path<(String, String)>) }); let body = hb.render("user", &data).unwrap(); - HttpResponse::Ok().body(body) + Html(body) } #[actix_web::main] async fn main() -> io::Result<()> { + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + // Handlebars uses a repository for the compiled templates. This object must be // shared between the application threads, and is therefore passed to the // Application Builder as an atomic reference-counted pointer. let mut handlebars = Handlebars::new(); handlebars - .register_templates_directory(".html", "./static/templates") + .register_templates_directory( + "./templates", + DirectorySourceOptions { + tpl_extension: ".html".to_owned(), + hidden: false, + temporary: false, + }, + ) .unwrap(); let handlebars_ref = web::Data::new(handlebars); @@ -52,6 +61,7 @@ async fn main() -> io::Result<()> { .service(index) .service(user) }) + .workers(2) .bind(("127.0.0.1", 8080))? .run() .await diff --git a/templating/handlebars/static/templates/error.html b/templating/handlebars/templates/error.html similarity index 100% rename from templating/handlebars/static/templates/error.html rename to templating/handlebars/templates/error.html diff --git a/templating/handlebars/static/templates/index.html b/templating/handlebars/templates/index.html similarity index 100% rename from templating/handlebars/static/templates/index.html rename to templating/handlebars/templates/index.html diff --git a/templating/handlebars/static/templates/user.html b/templating/handlebars/templates/user.html similarity index 100% rename from templating/handlebars/static/templates/user.html rename to templating/handlebars/templates/user.html