1
0
mirror of https://github.com/actix/examples synced 2024-11-27 07:52:57 +01: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
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
13 changed files with 27 additions and 40 deletions

10
Cargo.lock generated
View File

@ -423,9 +423,9 @@ dependencies = [
[[package]]
name = "actix-web"
version = "4.6.0"
version = "4.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1cf67dadb19d7c95e5a299e2dda24193b89d5d4f33a3b9800888ede9e19aa32"
checksum = "1988c02af8d2b718c05bc4aeb6a66395b7cdf32858c2c71131e5637a8c05a9ff"
dependencies = [
"actix-codec",
"actix-http",
@ -483,9 +483,9 @@ dependencies = [
[[package]]
name = "actix-web-codegen"
version = "4.2.2"
version = "4.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb1f50ebbb30eca122b188319a4398b3f7bb4a8cdf50ecfb73bfc6a3c3ce54f5"
checksum = "f591380e2e68490b5dfaf1dd1aa0ebe78d84ba7067078512b4ea6e4492d622b8"
dependencies = [
"actix-router",
"proc-macro2",
@ -4926,7 +4926,7 @@ dependencies = [
"actix-web",
"actix-web-lab",
"aes-gcm-siv",
"base64 0.22.1",
"base64 0.21.7",
"env_logger",
"log",
"serde",

View File

@ -90,7 +90,7 @@ actix-session = "0.9"
actix-test = "0.1"
actix-tls = "3.4"
actix-utils = "3"
actix-web = "4.6"
actix-web = "4.7"
actix-web-actors = "4.1"
actix-web-lab = "0.20"
actix-ws = "0.2.5"

View File

@ -2,15 +2,10 @@ use std::{fs, io};
use actix_multipart::form::{tempfile::TempFile, text::Text, MultipartForm};
use actix_web::{
body::SizedStream,
delete, error, get,
http::Method,
middleware::Logger,
post, route,
web::{self},
App, Error, HttpResponse, HttpServer, Responder,
body::SizedStream, delete, error, get, http::Method, middleware::Logger, post, route, web, App,
Error, HttpResponse, HttpServer, Responder,
};
use actix_web_lab::{extract::Path, respond::Html};
use actix_web_lab::extract::Path;
use aws_config::{meta::region::RegionProviderChain, BehaviorVersion};
use dotenvy::dotenv;
use futures_util::{stream, StreamExt as _};
@ -93,7 +88,7 @@ async fn delete_from_s3(
#[get("/")]
async fn index() -> impl Responder {
Html(include_str!("./index.html").to_owned())
web::Html::new(include_str!("./index.html").to_owned())
}
#[actix_web::main]

View File

@ -1,6 +1,5 @@
use actix_cors::Cors;
use actix_web::{get, middleware::Logger, route, web, App, HttpServer, Responder};
use actix_web_lab::respond::Html;
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
EmptyMutation, EmptySubscription, Schema,
@ -19,7 +18,7 @@ async fn graphql(schema: web::Data<StarWarsSchema>, req: GraphQLRequest) -> Grap
/// GraphiQL playground UI
#[get("/graphiql")]
async fn graphql_playground() -> impl Responder {
Html(playground_source(
web::Html::new(playground_source(
GraphQLPlaygroundConfig::new("/graphql").subscription_endpoint("/graphql"),
))
}

View File

@ -1,5 +1,4 @@
use actix_web::{get, route, web, Error, HttpResponse, Responder};
use actix_web_lab::respond::Html;
use juniper::http::{graphiql::graphiql_source, GraphQLRequest};
use crate::{
@ -26,7 +25,7 @@ pub async fn graphql(
/// GraphiQL UI
#[get("/graphiql")]
async fn graphql_playground() -> impl Responder {
Html(graphiql_source("/graphql", None))
web::Html::new(graphiql_source("/graphql", None))
}
pub fn register(config: &mut web::ServiceConfig) {

View File

@ -10,7 +10,6 @@ use actix_web::{
web::{self, Data},
App, HttpResponse, HttpServer, Responder,
};
use actix_web_lab::respond::Html;
use juniper::http::{graphiql::graphiql_source, GraphQLRequest};
mod schema;
@ -20,7 +19,7 @@ use crate::schema::{create_schema, Schema};
/// GraphiQL playground UI
#[get("/graphiql")]
async fn graphql_playground() -> impl Responder {
Html(graphiql_source("/graphql", None))
web::Html::new(graphiql_source("/graphql", None))
}
/// GraphQL endpoint

View File

@ -1,7 +1,7 @@
use std::{io, sync::Arc};
use actix_web::{get, middleware::Logger, post, web, App, HttpResponse, HttpServer, Responder};
use actix_web_lab::{extract::Path, respond::Html};
use actix_web_lab::extract::Path;
mod broadcast;
use self::broadcast::Broadcaster;
@ -30,7 +30,7 @@ async fn main() -> io::Result<()> {
#[get("/")]
async fn index() -> impl Responder {
Html(include_str!("index.html").to_owned())
web::Html::new(include_str!("index.html").to_owned())
}
#[get("/events")]

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]