1
0
mirror of https://github.com/actix/examples synced 2025-01-23 14:24:35 +01:00

130 lines
3.7 KiB
Rust
Raw Normal View History

2022-10-16 21:36:23 +01:00
use actix_utils::future::{ready, Ready};
2022-10-16 21:20:01 +01:00
use std::collections::HashMap;
use actix_web::{
2022-10-16 21:36:23 +01:00
dev::{self, ServiceResponse},
2022-10-16 21:20:01 +01:00
error,
http::{header::ContentType, StatusCode},
middleware::{ErrorHandlerResponse, ErrorHandlers, Logger},
2022-10-16 21:36:23 +01:00
web, App, FromRequest, HttpRequest, HttpResponse, HttpServer, Responder, Result,
2022-10-16 21:20:01 +01:00
};
use actix_web_lab::respond::Html;
2022-10-16 21:36:23 +01:00
#[derive(Debug)]
struct MiniJinjaRenderer {
2022-10-16 21:20:01 +01:00
tmpl_env: web::Data<minijinja::Environment<'static>>,
2022-10-16 21:36:23 +01:00
}
impl MiniJinjaRenderer {
fn render(
&self,
tmpl: &str,
ctx: impl Into<minijinja::value::Value>,
) -> actix_web::Result<Html> {
self.tmpl_env
.get_template(tmpl)
.map_err(|_| error::ErrorInternalServerError("could not find template"))?
.render(ctx.into())
.map(Html)
.map_err(|err| {
log::error!("{err}");
error::ErrorInternalServerError("template error")
})
}
}
impl FromRequest for MiniJinjaRenderer {
type Error = actix_web::Error;
type Future = Ready<Result<Self, Self::Error>>;
fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
let tmpl_env = <web::Data<minijinja::Environment<'static>>>::from_request(req, payload)
.into_inner()
.unwrap();
ready(Ok(Self { tmpl_env }))
}
}
async fn index(
tmpl_env: MiniJinjaRenderer,
2022-10-16 21:20:01 +01:00
query: web::Query<HashMap<String, String>>,
2022-10-16 21:36:23 +01:00
) -> actix_web::Result<impl Responder> {
if let Some(name) = query.get("name") {
tmpl_env.render(
"user.html",
minijinja::context! {
name,
text => "Welcome!",
},
)
2022-10-16 21:20:01 +01:00
} else {
2022-10-16 21:36:23 +01:00
tmpl_env.render("index.html", ())
}
2022-10-16 21:20:01 +01:00
}
#[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");
let mut env: minijinja::Environment<'static> = minijinja::Environment::new();
env.set_source(minijinja::Source::from_path(concat!(
env!("CARGO_MANIFEST_DIR"),
"/templates"
)));
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(env.clone()))
.service(web::resource("/").route(web::get().to(index)))
.wrap(ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found))
.wrap(Logger::default())
})
.workers(2)
.bind(("127.0.0.1", 8080))?
.run()
.await
}
/// Error handler for a 404 Page not found error.
fn not_found<B>(svc_res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
let res = get_error_response(&svc_res, "Page not found");
Ok(ErrorHandlerResponse::Response(ServiceResponse::new(
svc_res.into_parts().0,
res.map_into_right_body(),
)))
}
/// Generic error handler.
fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> HttpResponse {
let req = res.request();
2022-10-16 21:36:23 +01:00
let tmpl_env = MiniJinjaRenderer::extract(req).into_inner().unwrap();
2022-10-16 21:20:01 +01:00
// Provide a fallback to a simple plain text response in case an error occurs during the
// rendering of the error page.
let fallback = |err: &str| {
HttpResponse::build(res.status())
.content_type(ContentType::plaintext())
.body(err.to_string())
};
let ctx = minijinja::context! {
error => error,
status_code => res.status().as_str(),
};
2022-10-16 21:36:23 +01:00
match tmpl_env.render("error.html", ctx) {
Ok(body) => body
2022-10-16 21:20:01 +01:00
.customize()
.with_status(res.status())
2022-10-16 21:36:23 +01:00
.respond_to(req)
2022-10-16 21:20:01 +01:00
.map_into_boxed_body(),
2022-10-16 21:36:23 +01:00
Err(_) => fallback(error),
2022-10-16 21:20:01 +01:00
}
}