1
0
mirror of https://github.com/actix/examples synced 2025-06-26 17:17:42 +02:00

refactor: use graduated Html responder

This commit is contained in:
Rob Ede
2024-07-07 00:23:03 +01:00
parent 10aff3cdb1
commit a67c7803e6
13 changed files with 27 additions and 40 deletions

View File

@ -1,7 +1,6 @@
use std::collections::HashMap;
use actix_web::{middleware, web, App, HttpServer, Responder, Result};
use actix_web_lab::respond::Html;
use askama::Template;
#[derive(Template)]
@ -27,7 +26,7 @@ async fn index(query: web::Query<HashMap<String, String>>) -> Result<impl Respon
Index.render().expect("template should be valid")
};
Ok(Html(html))
Ok(web::Html::new(html))
}
#[actix_web::main]

View File

@ -8,7 +8,7 @@ use actix_web::{
middleware::{ErrorHandlerResponse, ErrorHandlers},
web, App, HttpResponse, HttpServer, Responder, Result,
};
use actix_web_lab::{extract::Path, respond::Html};
use actix_web_lab::extract::Path;
use fluent_templates::{static_loader, FluentLoader, Loader as _};
use handlebars::{DirectorySourceOptions, Handlebars};
use serde_json::json;
@ -31,7 +31,7 @@ static_loader! {
async fn index(hb: web::Data<Handlebars<'_>>, lang: LangChoice) -> impl Responder {
let data = json!({ "lang": lang });
let body = hb.render("index", &data).unwrap();
Html(body)
web::Html::new(body)
}
#[get("/{user}/{data}")]
@ -46,7 +46,7 @@ async fn user(
"data": info.1
});
let body = hb.render("user", &data).unwrap();
Html(body)
web::Html::new(body)
}
#[actix_web::main]

View File

@ -8,7 +8,6 @@ use actix_web::{
middleware::{ErrorHandlerResponse, ErrorHandlers},
web, App, HttpResponse, HttpServer, Responder, Result,
};
use actix_web_lab::respond::Html;
use handlebars::{DirectorySourceOptions, Handlebars};
use serde_json::json;
@ -19,7 +18,7 @@ async fn index(hb: web::Data<Handlebars<'_>>) -> impl Responder {
});
let body = hb.render("index", &data).unwrap();
Html(body)
web::Html::new(body)
}
#[get("/{user}/{data}")]
@ -31,7 +30,7 @@ async fn user(hb: web::Data<Handlebars<'_>>, path: web::Path<(String, String)>)
});
let body = hb.render("user", &data).unwrap();
Html(body)
web::Html::new(body)
}
#[actix_web::main]

View File

@ -8,7 +8,6 @@ use actix_web::{
middleware::{ErrorHandlerResponse, ErrorHandlers, Logger},
web, App, FromRequest, HttpRequest, HttpResponse, HttpServer, Responder, Result,
};
use actix_web_lab::respond::Html;
use minijinja::path_loader;
use minijinja_autoreload::AutoReloader;
@ -21,14 +20,14 @@ impl MiniJinjaRenderer {
&self,
tmpl: &str,
ctx: impl Into<minijinja::value::Value>,
) -> actix_web::Result<Html> {
) -> actix_web::Result<impl Responder> {
self.tmpl_env
.acquire_env()
.map_err(|_| error::ErrorInternalServerError("could not acquire template env"))?
.get_template(tmpl)
.map_err(|_| error::ErrorInternalServerError("could not find template"))?
.render(ctx.into())
.map(Html)
.map(web::Html::new)
.map_err(|err| {
log::error!("{err}");
error::ErrorInternalServerError("template error")

View File

@ -3,7 +3,6 @@ use actix_web::{
middleware::{Compress, Logger},
web, App, HttpServer, Responder,
};
use actix_web_lab::respond::Html;
use sailfish::TemplateOnce;
#[derive(TemplateOnce)]
@ -24,7 +23,7 @@ async fn greet(params: web::Path<(String,)>) -> actix_web::Result<impl Responder
.render_once()
.map_err(error::ErrorInternalServerError)?;
Ok(Html(body))
Ok(web::Html::new(body))
}
#[get("/page-{id:\\d+}")]
@ -33,12 +32,12 @@ async fn page(params: web::Path<(i32,)>) -> actix_web::Result<impl Responder> {
.render_once()
.map_err(error::ErrorInternalServerError)?;
Ok(Html(body))
Ok(web::Html::new(body))
}
#[get("/")]
async fn hello() -> impl Responder {
Html("<p>Hello world!</p>".to_owned())
web::Html::new("<p>Hello world!</p>".to_owned())
}
#[actix_web::main]

View File

@ -8,7 +8,6 @@ use actix_web::{
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
@ -28,7 +27,7 @@ async fn index(
.map_err(|_| error::ErrorInternalServerError("Template error"))?
};
Ok(Html(s))
Ok(web::Html::new(s))
}
#[actix_web::main]