1
0
mirror of https://github.com/actix/examples synced 2024-12-18 08:23:10 +01:00
examples/template_engines/tera/src/main.rs

94 lines
3.3 KiB
Rust
Raw Normal View History

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::{ErrorHandlerResponse, ErrorHandlers};
use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer, Result};
use std::collections::HashMap;
2019-12-18 19:25:56 +01:00
use tera::Tera;
2019-03-10 07:38:15 +01:00
// store tera template in application state
2019-12-07 18:59:24 +01:00
async fn index(
2019-03-17 04:23:09 +01:00
tmpl: web::Data<tera::Tera>,
2019-03-10 07:38:15 +01:00
query: web::Query<HashMap<String, String>>,
2018-05-08 20:08:43 +02:00
) -> Result<HttpResponse, Error> {
let s = if let Some(name) = query.get("name") {
2019-03-10 07:38:15 +01:00
// submitted form
let mut ctx = tera::Context::new();
2019-03-10 07:38:15 +01:00
ctx.insert("name", &name.to_owned());
ctx.insert("text", &"Welcome!".to_owned());
tmpl.render("user.html", &ctx)
.map_err(|_| error::ErrorInternalServerError("Template error"))?
} else {
2019-03-10 07:38:15 +01:00
tmpl.render("index.html", &tera::Context::new())
.map_err(|_| error::ErrorInternalServerError("Template error"))?
};
2018-05-08 20:08:43 +02:00
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
2020-09-12 17:49:45 +02:00
#[actix_web::main]
2019-12-07 18:59:24 +01:00
async fn main() -> std::io::Result<()> {
2019-03-10 07:38:15 +01:00
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
println!("Listening on: 127.0.0.1:8080, open browser and visit have a try!");
2019-03-10 07:38:15 +01:00
HttpServer::new(|| {
2018-05-08 20:08:43 +02:00
let tera =
2019-12-18 19:25:56 +01:00
Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")).unwrap();
2019-03-10 07:38:15 +01:00
App::new()
.app_data(web::Data::new(tera))
2019-03-26 04:29:00 +01:00
.wrap(middleware::Logger::default()) // enable logger
2019-03-10 07:38:15 +01:00
.service(web::resource("/").route(web::get().to(index)))
.service(web::scope("").wrap(error_handlers()))
2019-03-10 03:03:09 +01:00
})
2019-03-10 07:38:15 +01:00
.bind("127.0.0.1:8080")?
2019-12-25 17:48:33 +01:00
.run()
2019-12-07 18:59:24 +01:00
.await
}
// Custom error handlers, to return HTML responses when an error occurs.
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<BoxBody>> {
let response = get_error_response(&res, "Page not found");
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) -> 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| {
HttpResponse::build(res.status())
.content_type(ContentType::plaintext())
.body(e.to_string())
};
let tera = request.app_data::<web::Data<Tera>>().map(|t| t.get_ref());
match tera {
Some(tera) => {
let mut context = tera::Context::new();
context.insert("error", error);
context.insert("status_code", res.status().as_str());
let body = tera.render("error.html", &context);
match body {
Ok(body) => HttpResponse::build(res.status())
.content_type(ContentType::html())
.body(body),
Err(_) => fallback(error),
}
}
None => fallback(error),
}
}