mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
commit
53c778d436
@ -34,7 +34,8 @@ members = [
|
||||
"rustls",
|
||||
"shutdown-server",
|
||||
"server-sent-events",
|
||||
"simple-auth-server",
|
||||
# Depency clang outdate bindgen@0.48
|
||||
# "simple-auth-server",
|
||||
"state",
|
||||
"static_index",
|
||||
"template_askama",
|
||||
|
@ -10,10 +10,12 @@ workspace = ".."
|
||||
[dependencies]
|
||||
env_logger = "0.7"
|
||||
|
||||
yarte = { version = "0.7", features = ["with-actix-web"] }
|
||||
yarte = { version = "0.8", features = ["html-min"] }
|
||||
|
||||
actix-rt = "1.0"
|
||||
actix-web = "2.0.0"
|
||||
|
||||
[build-dependencies]
|
||||
yarte = { version = "0.7", features = ["with-actix-web"] }
|
||||
[build-dependencies.yarte_helpers]
|
||||
version = "0.8"
|
||||
default-features = false
|
||||
features = ["config"]
|
||||
|
@ -1,3 +1,3 @@
|
||||
fn main() {
|
||||
yarte::recompile::when_changed();
|
||||
yarte_helpers::recompile::when_changed();
|
||||
}
|
||||
|
@ -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()
|
||||
)
|
||||
);
|
||||
|
@ -3,10 +3,7 @@
|
||||
!}}
|
||||
{{> ../deep/welcome id = "welcome", tag = "h1", tail = '!' ~}}
|
||||
<div>
|
||||
{{! Title !}}
|
||||
<h3>What is your name?</h3>
|
||||
|
||||
{{! Form !}}
|
||||
<form>
|
||||
{{! Input name !}}
|
||||
Name: <input type="text" name="name" /><br/>
|
||||
|
@ -5,4 +5,7 @@
|
||||
- id
|
||||
- tail
|
||||
!}}
|
||||
{{#unless tag.is_match(r"^p|(h[1-6])$") && !id.is_empty() }}
|
||||
{{$ "Need static args: tag: str /^h[1-6]$/, id: str" }}
|
||||
{{/unless }}
|
||||
<{{ tag }} id="{{ id }}" class="welcome">Welcome{{ tail }}</{{ tag }}>
|
||||
|
@ -1,3 +1,6 @@
|
||||
{{# unless title.is_str() && !title.is_empty() }}
|
||||
{{$ "Need static args: title: str" }}
|
||||
{{/unless}}
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>{{ title }}</title>
|
||||
|
Loading…
Reference in New Issue
Block a user