1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +01:00

Update template-engines/tinytemplate to v4 (#501)

This commit is contained in:
Luca Palmieri 2022-02-02 15:18:48 +00:00 committed by GitHub
parent f9b0dde4c8
commit 9c620d1943
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 19 deletions

5
Cargo.lock generated
View File

@ -5806,9 +5806,8 @@ dependencies = [
name = "template-tinytemplate"
version = "1.0.0"
dependencies = [
"actix-http 2.2.2",
"actix-web 3.3.3",
"env_logger 0.8.4",
"actix-web 4.0.0-beta.21",
"env_logger 0.9.0",
"serde_json",
"tinytemplate",
]

View File

@ -4,8 +4,7 @@ version = "1.0.0"
edition = "2021"
[dependencies]
env_logger = "0.8"
env_logger = "0.9.0"
tinytemplate = "1.1"
actix-http = "2"
actix-web = "3"
actix-web = "4.0.0-beta.21"
serde_json = "1"

View File

@ -1,9 +1,10 @@
use std::collections::HashMap;
use actix_http::{body::Body, Response};
use actix_web::body::BoxBody;
use actix_web::dev::ServiceResponse;
use actix_web::http::header::ContentType;
use actix_web::http::StatusCode;
use actix_web::middleware::errhandlers::{ErrorHandlerResponse, ErrorHandlers};
use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers};
use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer, Result};
use serde_json::json;
use tinytemplate::TinyTemplate;
@ -40,7 +41,7 @@ async fn main() -> std::io::Result<()> {
tt.add_template("error.html", ERROR).unwrap();
App::new()
.data(tt)
.app_data(web::Data::new(tt))
.wrap(middleware::Logger::default()) // enable logger
.service(web::resource("/").route(web::get().to(index)))
.service(web::scope("").wrap(error_handlers()))
@ -51,27 +52,28 @@ async fn main() -> std::io::Result<()> {
}
// 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)
}
// 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");
Ok(ErrorHandlerResponse::Response(
res.into_response(response.into_body()),
))
Ok(ErrorHandlerResponse::Response(ServiceResponse::new(
res.into_parts().0,
response.map_into_left_body(),
)))
}
// 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 {
let request = res.request();
// Provide a fallback to a simple plain text response in case an error occurs during the
// rendering of the error page.
let fallback = |e: &str| {
Response::build(res.status())
.content_type("text/plain")
HttpResponse::build(res.status())
.content_type(ContentType::plaintext())
.body(e.to_string())
};
@ -86,8 +88,8 @@ fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> Response<Body
let body = tt.render("error.html", &context);
match body {
Ok(body) => Response::build(res.status())
.content_type("text/html")
Ok(body) => HttpResponse::build(res.status())
.content_type(ContentType::html())
.body(body),
Err(_) => fallback(error),
}