use std::collections::HashMap; use actix_web::{ body::BoxBody, dev::ServiceResponse, error, http::{header::ContentType, StatusCode}, middleware::{self, ErrorHandlerResponse, ErrorHandlers}, web, App, Error, HttpResponse, HttpServer, Responder, Result, }; use actix_web_lab::respond::Html; use tera::Tera; // store tera template in application state async fn index( tmpl: web::Data, query: web::Query>, ) -> Result { let s = if let Some(name) = query.get("name") { // submitted form let mut ctx = tera::Context::new(); ctx.insert("name", name); ctx.insert("text", "Welcome!"); tmpl.render("user.html", &ctx) .map_err(|_| error::ErrorInternalServerError("Template error"))? } else { tmpl.render("index.html", &tera::Context::new()) .map_err(|_| error::ErrorInternalServerError("Template error"))? }; Ok(Html(s)) } #[actix_web::main] async fn main() -> std::io::Result<()> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); log::info!("starting HTTP server at http://localhost:8080"); HttpServer::new(|| { let tera = Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")).unwrap(); App::new() .app_data(web::Data::new(tera)) .wrap(middleware::Logger::default()) .service(web::resource("/").route(web::get().to(index))) .service(web::scope("").wrap(error_handlers())) }) .bind(("127.0.0.1", 8080))? .run() .await } // Custom error handlers, to return HTML responses when an error occurs. fn error_handlers() -> ErrorHandlers { ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found) } // Error handler for a 404 Page not found error. fn not_found(res: ServiceResponse) -> Result> { 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(res: &ServiceResponse, 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::>().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), } }