mirror of
https://github.com/actix/examples
synced 2024-11-27 16:02:57 +01:00
chore: fix template location
This commit is contained in:
parent
e5fc327af0
commit
c5c208eda7
3
Cargo.lock
generated
3
Cargo.lock
generated
@ -7614,6 +7614,7 @@ version = "1.0.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"actix-web-lab",
|
"actix-web-lab",
|
||||||
|
"env_logger",
|
||||||
"fluent-templates",
|
"fluent-templates",
|
||||||
"handlebars 5.1.0",
|
"handlebars 5.1.0",
|
||||||
"serde",
|
"serde",
|
||||||
@ -7625,6 +7626,8 @@ name = "templating-handlebars"
|
|||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-web",
|
"actix-web",
|
||||||
|
"actix-web-lab",
|
||||||
|
"env_logger",
|
||||||
"handlebars 5.1.0",
|
"handlebars 5.1.0",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
]
|
]
|
||||||
|
@ -6,7 +6,8 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web.workspace = true
|
actix-web.workspace = true
|
||||||
actix-web-lab.workspace = true
|
actix-web-lab.workspace = true
|
||||||
|
env_logger.workspace = true
|
||||||
fluent-templates = { version = "0.9", features = ["handlebars"] }
|
fluent-templates = { version = "0.9", features = ["handlebars"] }
|
||||||
handlebars = { version = "5.1", features = ["dir_source"] }
|
handlebars = { version = "5", features = ["dir_source"] }
|
||||||
serde.workspace = true
|
serde.workspace = true
|
||||||
serde_json.workspace = true
|
serde_json.workspace = true
|
||||||
|
@ -51,6 +51,8 @@ async fn user(
|
|||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> io::Result<()> {
|
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
|
// 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.
|
// the application threads, and is therefore passed to the App in an Arc.
|
||||||
let mut handlebars = Handlebars::new();
|
let mut handlebars = Handlebars::new();
|
||||||
@ -58,7 +60,7 @@ async fn main() -> io::Result<()> {
|
|||||||
// register template dir with Handlebars registry
|
// register template dir with Handlebars registry
|
||||||
handlebars
|
handlebars
|
||||||
.register_templates_directory(
|
.register_templates_directory(
|
||||||
"./static/templates",
|
"./templates",
|
||||||
DirectorySourceOptions {
|
DirectorySourceOptions {
|
||||||
tpl_extension: ".html".to_owned(),
|
tpl_extension: ".html".to_owned(),
|
||||||
hidden: false,
|
hidden: false,
|
||||||
@ -79,6 +81,7 @@ async fn main() -> io::Result<()> {
|
|||||||
.service(index)
|
.service(index)
|
||||||
.service(user)
|
.service(user)
|
||||||
})
|
})
|
||||||
|
.workers(2)
|
||||||
.bind(("127.0.0.1", 8080))?
|
.bind(("127.0.0.1", 8080))?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
|
@ -5,5 +5,7 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web.workspace = true
|
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
|
serde_json.workspace = true
|
||||||
|
@ -6,24 +6,24 @@ use actix_web::{
|
|||||||
get,
|
get,
|
||||||
http::{header::ContentType, StatusCode},
|
http::{header::ContentType, StatusCode},
|
||||||
middleware::{ErrorHandlerResponse, ErrorHandlers},
|
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 handlebars::{DirectorySourceOptions, Handlebars};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
// Macro documentation can be found in the actix_web_codegen crate
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
async fn index(hb: web::Data<Handlebars<'_>>) -> HttpResponse {
|
async fn index(hb: web::Data<Handlebars<'_>>) -> impl Responder {
|
||||||
let data = json!({
|
let data = json!({
|
||||||
"name": "Handlebars"
|
"name": "Handlebars"
|
||||||
});
|
});
|
||||||
let body = hb.render("index", &data).unwrap();
|
let body = hb.render("index", &data).unwrap();
|
||||||
|
|
||||||
HttpResponse::Ok().body(body)
|
Html(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/{user}/{data}")]
|
#[get("/{user}/{data}")]
|
||||||
async fn user(hb: web::Data<Handlebars<'_>>, path: web::Path<(String, String)>) -> HttpResponse {
|
async fn user(hb: web::Data<Handlebars<'_>>, path: web::Path<(String, String)>) -> impl Responder {
|
||||||
let info = path.into_inner();
|
let info = path.into_inner();
|
||||||
let data = json!({
|
let data = json!({
|
||||||
"user": info.0,
|
"user": info.0,
|
||||||
@ -31,18 +31,20 @@ async fn user(hb: web::Data<Handlebars<'_>>, path: web::Path<(String, String)>)
|
|||||||
});
|
});
|
||||||
let body = hb.render("user", &data).unwrap();
|
let body = hb.render("user", &data).unwrap();
|
||||||
|
|
||||||
HttpResponse::Ok().body(body)
|
Html(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> io::Result<()> {
|
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
|
// Handlebars uses a repository for the compiled templates. This object must be
|
||||||
// shared between the application threads, and is therefore passed to the
|
// shared between the application threads, and is therefore passed to the
|
||||||
// Application Builder as an atomic reference-counted pointer.
|
// Application Builder as an atomic reference-counted pointer.
|
||||||
let mut handlebars = Handlebars::new();
|
let mut handlebars = Handlebars::new();
|
||||||
handlebars
|
handlebars
|
||||||
.register_templates_directory(
|
.register_templates_directory(
|
||||||
"./static/templates",
|
"./templates",
|
||||||
DirectorySourceOptions {
|
DirectorySourceOptions {
|
||||||
tpl_extension: ".html".to_owned(),
|
tpl_extension: ".html".to_owned(),
|
||||||
hidden: false,
|
hidden: false,
|
||||||
@ -59,6 +61,7 @@ async fn main() -> io::Result<()> {
|
|||||||
.service(index)
|
.service(index)
|
||||||
.service(user)
|
.service(user)
|
||||||
})
|
})
|
||||||
|
.workers(2)
|
||||||
.bind(("127.0.0.1", 8080))?
|
.bind(("127.0.0.1", 8080))?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
|
Loading…
Reference in New Issue
Block a user