From c5c208eda73afcaf49931b168f517ba4190ca2b4 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Tue, 5 Mar 2024 22:30:51 +0000 Subject: [PATCH] chore: fix template location --- Cargo.lock | 3 +++ templating/fluent/Cargo.toml | 3 ++- templating/fluent/src/main.rs | 5 ++++- templating/handlebars/Cargo.toml | 4 +++- templating/handlebars/src/main.rs | 17 ++++++++++------- .../{static => }/templates/error.html | 0 .../{static => }/templates/index.html | 0 .../handlebars/{static => }/templates/user.html | 0 8 files changed, 22 insertions(+), 10 deletions(-) rename templating/handlebars/{static => }/templates/error.html (100%) rename templating/handlebars/{static => }/templates/index.html (100%) rename templating/handlebars/{static => }/templates/user.html (100%) diff --git a/Cargo.lock b/Cargo.lock index 4e30c3a..29e9c59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7614,6 +7614,7 @@ version = "1.0.0" dependencies = [ "actix-web", "actix-web-lab", + "env_logger", "fluent-templates", "handlebars 5.1.0", "serde", @@ -7625,6 +7626,8 @@ name = "templating-handlebars" version = "1.0.0" dependencies = [ "actix-web", + "actix-web-lab", + "env_logger", "handlebars 5.1.0", "serde_json", ] diff --git a/templating/fluent/Cargo.toml b/templating/fluent/Cargo.toml index e1a73a2..53b29e0 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 +env_logger.workspace = true fluent-templates = { version = "0.9", features = ["handlebars"] } -handlebars = { version = "5.1", features = ["dir_source"] } +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 9cd7874..d8c6cde 100644 --- a/templating/fluent/src/main.rs +++ b/templating/fluent/src/main.rs @@ -51,6 +51,8 @@ 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(); @@ -58,7 +60,7 @@ async fn main() -> io::Result<()> { // register template dir with Handlebars registry handlebars .register_templates_directory( - "./static/templates", + "./templates", DirectorySourceOptions { tpl_extension: ".html".to_owned(), hidden: false, @@ -79,6 +81,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/Cargo.toml b/templating/handlebars/Cargo.toml index 7b64c60..4079d49 100644 --- a/templating/handlebars/Cargo.toml +++ b/templating/handlebars/Cargo.toml @@ -5,5 +5,7 @@ edition = "2021" [dependencies] actix-web.workspace = true -handlebars = { version = "5.1", 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 abffed2..ba36421 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 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,18 +31,20 @@ 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( - "./static/templates", + "./templates", DirectorySourceOptions { tpl_extension: ".html".to_owned(), hidden: false, @@ -59,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