1
0
mirror of https://github.com/actix/examples synced 2025-03-20 10:35:18 +01:00

Update template-engines/handlerbars to v4 (#497)

This commit is contained in:
Luca Palmieri 2022-02-02 15:17:17 +00:00 committed by GitHub
parent 32f6289764
commit 6afc431858
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 31 deletions

7
Cargo.lock generated
View File

@ -2736,9 +2736,9 @@ dependencies = [
[[package]] [[package]]
name = "handlebars" name = "handlebars"
version = "3.5.5" version = "4.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4498fc115fa7d34de968184e473529abb40eeb6be8bc5f7faba3d08c316cb3e3" checksum = "25546a65e5cf1f471f3438796fc634650b31d7fcde01d444c309aeb28b92e3a8"
dependencies = [ dependencies = [
"log", "log",
"pest", "pest",
@ -5818,8 +5818,7 @@ dependencies = [
name = "template_handlebars" name = "template_handlebars"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix-http 2.2.2", "actix-web 4.0.0-beta.21",
"actix-web 3.3.3",
"handlebars", "handlebars",
"serde_json", "serde_json",
] ]

View File

@ -4,7 +4,6 @@ version = "1.0.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
actix-http = "2" actix-web = "4.0.0-beta.21"
actix-web = "3" handlebars = { version = "4.2.1", features = ["dir_source"] }
handlebars = { version = "3.0.0", features = ["dir_source"] }
serde_json = "1.0" serde_json = "1.0"

View File

@ -1,17 +1,11 @@
#[macro_use] use actix_web::body::BoxBody;
extern crate actix_web;
#[macro_use]
extern crate serde_json;
use actix_http::{body::Body, Response};
use actix_web::dev::ServiceResponse; use actix_web::dev::ServiceResponse;
use actix_web::http::header::ContentType;
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
use actix_web::middleware::errhandlers::{ErrorHandlerResponse, ErrorHandlers}; use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers};
use actix_web::{web, App, HttpResponse, HttpServer, Result}; use actix_web::{get, web, App, HttpResponse, HttpServer, Result};
use handlebars::Handlebars; use handlebars::Handlebars;
use serde_json::json;
use std::io; use std::io;
// Macro documentation can be found in the actix_web_codegen crate // Macro documentation can be found in the actix_web_codegen crate
@ -28,8 +22,9 @@ async fn index(hb: web::Data<Handlebars<'_>>) -> HttpResponse {
#[get("/{user}/{data}")] #[get("/{user}/{data}")]
async fn user( async fn user(
hb: web::Data<Handlebars<'_>>, hb: web::Data<Handlebars<'_>>,
web::Path(info): web::Path<(String, String)>, path: web::Path<(String, String)>,
) -> HttpResponse { ) -> HttpResponse {
let info = path.into_inner();
let data = json!({ let data = json!({
"user": info.0, "user": info.0,
"data": info.1 "data": info.1
@ -63,27 +58,31 @@ async fn main() -> io::Result<()> {
} }
// Custom error handlers, to return HTML responses when an error occurs. // Custom error handlers, to return HTML responses when an error occurs.
fn error_handlers() -> ErrorHandlers<Body> { fn error_handlers() -> ErrorHandlers<BoxBody> {
ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found) ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found)
} }
// Error handler for a 404 Page not found error. // Error handler for a 404 Page not found error.
fn not_found<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> { fn not_found<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<BoxBody>> {
let response = get_error_response(&res, "Page not found"); let response = get_error_response(&res, "Page not found");
Ok(ErrorHandlerResponse::Response( Ok(ErrorHandlerResponse::Response(ServiceResponse::new(
res.into_response(response.into_body()), res.into_parts().0,
)) response.map_into_left_body(),
)))
} }
// Generic error handler. // Generic error handler.
fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> Response<Body> { fn get_error_response<B>(
res: &ServiceResponse<B>,
error: &str,
) -> HttpResponse<BoxBody> {
let request = res.request(); let request = res.request();
// Provide a fallback to a simple plain text response in case an error occurs during the // Provide a fallback to a simple plain text response in case an error occurs during the
// rendering of the error page. // rendering of the error page.
let fallback = |e: &str| { let fallback = |e: &str| {
Response::build(res.status()) HttpResponse::build(res.status())
.content_type("text/plain") .content_type(ContentType::plaintext())
.body(e.to_string()) .body(e.to_string())
}; };
@ -99,12 +98,12 @@ fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> Response<Body
let body = hb.render("error", &data); let body = hb.render("error", &data);
match body { match body {
Ok(body) => Response::build(res.status()) Ok(body) => HttpResponse::build(res.status())
.content_type("text/html") .content_type(ContentType::html())
.body(body), .body(body),
Err(_) => fallback(error), Err(_) => fallback(error).into(),
} }
} }
None => fallback(error), None => fallback(error).into(),
} }
} }