mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
restructure folders
This commit is contained in:
16
templating/yarte/Cargo.toml
Normal file
16
templating/yarte/Cargo.toml
Normal file
@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "template_yarte"
|
||||
version = "1.0.0"
|
||||
publish = false
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4.0.0-rc.1"
|
||||
env_logger = "0.9.0"
|
||||
yarte = { version = "0.15", features = ["bytes-buf", "html-min"] }
|
||||
derive_more = "0.99"
|
||||
|
||||
[build-dependencies.yarte_helpers]
|
||||
version = "0.15.6"
|
||||
default-features = false
|
||||
features = ["config"]
|
10
templating/yarte/README.md
Normal file
10
templating/yarte/README.md
Normal file
@ -0,0 +1,10 @@
|
||||
# yarte
|
||||
|
||||
Minimal example of using template [yarte](https://github.com/botika/yarte) that displays a form.
|
||||
|
||||
```bash
|
||||
cd template_engines/yarte
|
||||
cargo test
|
||||
cargo run
|
||||
```
|
||||
> open `localhost:8080`
|
3
templating/yarte/build.rs
Normal file
3
templating/yarte/build.rs
Normal file
@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
yarte_helpers::recompile::when_changed();
|
||||
}
|
124
templating/yarte/src/main.rs
Normal file
124
templating/yarte/src/main.rs
Normal file
@ -0,0 +1,124 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use actix_web::{
|
||||
get, middleware::Logger, web, App, Error, HttpResponse, HttpServer, ResponseError,
|
||||
};
|
||||
use derive_more::Display;
|
||||
use yarte::{auto, ywrite_min};
|
||||
|
||||
#[derive(Debug, Display)]
|
||||
struct MyErr(pub &'static str);
|
||||
|
||||
impl ResponseError for MyErr {}
|
||||
|
||||
#[allow(unused_must_use)] // ywrite_min causes warning: unused borrow that must be used
|
||||
#[get("/")]
|
||||
async fn index(
|
||||
query: web::Query<HashMap<String, String>>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
// `ywrite_min` is work in progress check your templates before put in production
|
||||
// or use `ywrite_html`
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(auto!(ywrite_min!(String, "{{> index }}"))))
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
env_logger::init();
|
||||
|
||||
// start http server
|
||||
HttpServer::new(move || App::new().wrap(Logger::default()).service(index))
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use actix_web::{http, test as atest, web::Bytes};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test() {
|
||||
let app = atest::init_service(App::new().service(index)).await;
|
||||
|
||||
let req = atest::TestRequest::with_uri("/").to_request();
|
||||
let resp = atest::call_service(&app, req).await;
|
||||
|
||||
assert!(resp.status().is_success());
|
||||
|
||||
assert_eq!(
|
||||
resp.headers().get(http::header::CONTENT_TYPE).unwrap(),
|
||||
"text/html; charset=utf-8"
|
||||
);
|
||||
|
||||
let bytes = atest::read_body(resp).await;
|
||||
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>"
|
||||
.as_ref()
|
||||
)
|
||||
);
|
||||
|
||||
let req = atest::TestRequest::with_uri("/?name=foo&lastname=bar").to_request();
|
||||
let resp = atest::call_service(&app, req).await;
|
||||
|
||||
assert!(resp.status().is_success());
|
||||
|
||||
assert_eq!(
|
||||
resp.headers().get(http::header::CONTENT_TYPE).unwrap(),
|
||||
"text/html; charset=utf-8"
|
||||
);
|
||||
|
||||
let bytes = atest::read_body(resp).await;
|
||||
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>"
|
||||
.as_ref()
|
||||
)
|
||||
);
|
||||
|
||||
let req = atest::TestRequest::with_uri("/?name=foo").to_request();
|
||||
let resp = atest::call_service(&app, req).await;
|
||||
|
||||
assert!(resp.status().is_server_error());
|
||||
|
||||
let bytes = atest::read_body(resp).await;
|
||||
|
||||
assert_eq!(bytes, Bytes::from_static("Bad query".as_ref()));
|
||||
|
||||
let req = atest::TestRequest::with_uri("/?lastname=bar").to_request();
|
||||
let resp = atest::call_service(&app, req).await;
|
||||
|
||||
assert!(resp.status().is_success());
|
||||
|
||||
assert_eq!(
|
||||
resp.headers().get(http::header::CONTENT_TYPE).unwrap(),
|
||||
"text/html; charset=utf-8"
|
||||
);
|
||||
|
||||
let bytes = atest::read_body(resp).await;
|
||||
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>"
|
||||
.as_ref()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
8
templating/yarte/templates/base.hbs
Normal file
8
templating/yarte/templates/base.hbs
Normal file
@ -0,0 +1,8 @@
|
||||
{{! Simple example !}}
|
||||
{{> doc/t }}
|
||||
<html>
|
||||
{{> doc/head }}
|
||||
<body>
|
||||
{{> @partial-block }}
|
||||
</body>
|
||||
</html>
|
16
templating/yarte/templates/deep/more/card/form.hbs
Normal file
16
templating/yarte/templates/deep/more/card/form.hbs
Normal file
@ -0,0 +1,16 @@
|
||||
{{!
|
||||
Form: What is your name?
|
||||
!}}
|
||||
{{> ../deep/welcome id = "welcome", tag = "h1", tail = '!' ~}}
|
||||
<div>
|
||||
<h3>What is your name?</h3>
|
||||
<form>
|
||||
{{! Input name !}}
|
||||
Name: <input type="text" name="name" /><br/>
|
||||
{{! Input last name !}}
|
||||
Last name: <input type="text" name="lastname"/><br/>
|
||||
<p>
|
||||
<input type="submit">
|
||||
</p>
|
||||
</form>
|
||||
</div>
|
8
templating/yarte/templates/deep/more/card/hi.hbs
Normal file
8
templating/yarte/templates/deep/more/card/hi.hbs
Normal file
@ -0,0 +1,8 @@
|
||||
{{!
|
||||
Hi message:
|
||||
args:
|
||||
- lastname
|
||||
- name
|
||||
!}}
|
||||
<h1>Hi, {{ name }} {{ lastname }}!</h1>
|
||||
{{~> alias/welcome id = "hi", tag = 'p', tail = "" }}
|
11
templating/yarte/templates/deep/more/deep/welcome.hbs
Normal file
11
templating/yarte/templates/deep/more/deep/welcome.hbs
Normal file
@ -0,0 +1,11 @@
|
||||
{{!
|
||||
Welcome card:
|
||||
args:
|
||||
- tag
|
||||
- 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 }}>
|
7
templating/yarte/templates/deep/more/doc/head.hbs
Normal file
7
templating/yarte/templates/deep/more/doc/head.hbs
Normal file
@ -0,0 +1,7 @@
|
||||
{{# unless title.is_str() && !title.is_empty() }}
|
||||
{{$ "Need static args: title: str" }}
|
||||
{{/unless}}
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>{{ title }}</title>
|
||||
</head>
|
1
templating/yarte/templates/deep/more/doc/t.hbs
Normal file
1
templating/yarte/templates/deep/more/doc/t.hbs
Normal file
@ -0,0 +1 @@
|
||||
<!DOCTYPE html>
|
8
templating/yarte/templates/index.hbs
Normal file
8
templating/yarte/templates/index.hbs
Normal file
@ -0,0 +1,8 @@
|
||||
{{#> base title = "Actix Web" }}
|
||||
{{~#if let Some(name) = query.get("name") }}
|
||||
{{ let lastname = query.get("lastname").ok_or(MyErr("Bad query"))? }}
|
||||
{{> card/hi ~}}
|
||||
{{ else ~}}
|
||||
{{> card/form ~}}
|
||||
{{/if ~}}
|
||||
{{/base }}
|
9
templating/yarte/yarte.toml
Normal file
9
templating/yarte/yarte.toml
Normal file
@ -0,0 +1,9 @@
|
||||
# root dir of templates
|
||||
[main]
|
||||
dir = "templates"
|
||||
|
||||
# Alias for partials. In call, change the start of partial path with one of this, if exist.
|
||||
[partials]
|
||||
alias = "./deep/more/deep"
|
||||
doc = "./deep/more/doc"
|
||||
card = "./deep/more/card"
|
Reference in New Issue
Block a user