1
0
mirror of https://github.com/actix/examples synced 2025-09-02 03:46:38 +02:00

Update yarte version to 0.8

This commit is contained in:
Juan Aguilar Santillana
2020-04-28 21:18:18 +02:00
parent 8dab533b40
commit a65abad80d
7 changed files with 44 additions and 41 deletions

View File

@@ -1,17 +1,29 @@
use std::collections::HashMap;
use actix_web::{get, middleware::Logger, web, App, HttpServer, Responder};
use yarte::Template;
use actix_web::{
error::ErrorInternalServerError, get, middleware::Logger, web, App, Error,
HttpResponse, HttpServer,
};
use yarte::TemplateMin;
#[derive(Template)]
#[template(path = "index.hbs", err = "Some error message", mode = "html-min")]
#[derive(TemplateMin)]
#[template(path = "index")]
struct IndexTemplate {
query: web::Query<HashMap<String, String>>,
}
#[get("/")]
async fn index(query: web::Query<HashMap<String, String>>) -> impl Responder {
async fn index(
query: web::Query<HashMap<String, String>>,
) -> Result<HttpResponse, Error> {
IndexTemplate { query }
.call()
.map(|body| {
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(body)
})
.map_err(|_| ErrorInternalServerError("Some error message"))
}
#[actix_rt::main]
@@ -49,17 +61,11 @@ mod test {
assert_eq!(
bytes,
Bytes::from_static(
"<!DOCTYPE html>\
<html>\
<head><meta charset=\"utf-8\"><title>Actix web</title></head><body>\
<h1 id=\"welcome\" class=\"welcome\">Welcome!</h1><div>\
<h3>What is your name?</h3>\
<form>\
Name: <input type=\"text\" name=\"name\">\
<br>Last name: <input type=\"text\" name=\"lastname\">\
<br><p><input type=\"submit\"></p></form>\
</div>\
</body></html>"
"<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Actix \
web</title></head><body><h1 id=\"welcome\" \
class=\"welcome\">Welcome!</h1><div><h3>What is your name?</h3><form>Name: \
<input type=\"text\" name=\"name\"><br>Last name: <input type=\"text\" \
name=\"lastname\"><br><p><input type=\"submit\"></p></form></div></body></html>"
.as_ref()
)
);
@@ -78,12 +84,9 @@ mod test {
assert_eq!(
bytes,
Bytes::from_static(
"<!DOCTYPE html>\
<html>\
<head><meta charset=\"utf-8\"><title>Actix web</title></head>\
<body>\
<h1>Hi, foo bar!</h1><p id=\"hi\" class=\"welcome\">Welcome</p>\
</body></html>"
"<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Actix \
web</title></head><body><h1>Hi, foo bar!</h1><p id=\"hi\" \
class=\"welcome\">Welcome</p></body></html>"
.as_ref()
)
);
@@ -111,17 +114,11 @@ mod test {
assert_eq!(
bytes,
Bytes::from_static(
"<!DOCTYPE html>\
<html>\
<head><meta charset=\"utf-8\"><title>Actix web</title></head><body>\
<h1 id=\"welcome\" class=\"welcome\">Welcome!</h1><div>\
<h3>What is your name?</h3>\
<form>\
Name: <input type=\"text\" name=\"name\">\
<br>Last name: <input type=\"text\" name=\"lastname\">\
<br><p><input type=\"submit\"></p></form>\
</div>\
</body></html>"
"<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Actix \
web</title></head><body><h1 id=\"welcome\" \
class=\"welcome\">Welcome!</h1><div><h3>What is your name?</h3><form>Name: \
<input type=\"text\" name=\"name\"><br>Last name: <input type=\"text\" \
name=\"lastname\"><br><p><input type=\"submit\"></p></form></div></body></html>"
.as_ref()
)
);