1
0
mirror of https://github.com/actix/examples synced 2025-01-22 22:05:57 +01:00

Merge branch '1.0' of github.com:actix/examples into 1.0

This commit is contained in:
Nikolay Kim 2019-03-16 20:23:44 -07:00
commit d633a35fdd
10 changed files with 112 additions and 0 deletions

View File

@ -60,6 +60,7 @@ script:
cd static_index && cargo check && cd ..
cd template_askama && cargo check && cd ..
cd template_tera && cargo check && cd ..
cd template_yarte && cargo check && cd ..
cd tls && cargo check && cd ..
cd rustls && cargo check && cd ..
cd unix-socket && cargo check && cd ..

View File

@ -26,6 +26,7 @@ members = [
"static_index",
"template_askama",
"template_tera",
"template_yarte",
"tls",
"rustls",
"unix-socket",

17
template_yarte/Cargo.toml Normal file
View File

@ -0,0 +1,17 @@
[package]
name = "example"
version = "0.0.1"
authors = ["Rust-iendo Barcelona <riendocontributions@gmail.com>"]
publish = false
edition = "2018"
workspace = ".."
[dependencies]
env_logger = "0.6"
yarte = "0.1"
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
[build-dependencies]
yarte = "0.1"

11
template_yarte/README.md Normal file
View File

@ -0,0 +1,11 @@
# yarte
Example of composition with partials and `with-actix-web` feature
See the generated code on stdout when run at debug
```bash
cargo run
```
> open `localhost:8080`
More at [mdbook](https://yarte.netlify.com/) and [repository](https://gitlab.com/r-iendo/yarte)

5
template_yarte/build.rs Normal file
View File

@ -0,0 +1,5 @@
use yarte::recompile;
fn main() {
recompile::when_changed();
}

21
template_yarte/src/lib.rs Normal file
View File

@ -0,0 +1,21 @@
use actix_web::{error::ErrorInternalServerError, web::Query, HttpResponse, Result};
use yarte::Template;
use std::collections::HashMap;
#[derive(Template)]
#[template(path = "index.hbs")]
struct IndexTemplate {
query: Query<HashMap<String, String>>,
}
pub fn index(query: Query<HashMap<String, String>>) -> Result<HttpResponse> {
IndexTemplate { query }
.call()
.map(|s| {
HttpResponse::Ok()
.content_type(IndexTemplate::mime())
.body(s)
})
.map_err(|_| ErrorInternalServerError("Template parsing error"))
}

View File

@ -0,0 +1,19 @@
use actix_web::{middleware, web, App, HttpServer};
#[path = "lib.rs"]
mod template;
fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
// start http server
HttpServer::new(|| {
App::new()
// enable logger
.middleware(middleware::Logger::default())
.service(web::resource("/").to(template::index))
})
.bind("127.0.0.1:8080")?
.run()
}

View File

@ -0,0 +1 @@
<{{ tag }}>Welcome!</{{ tag }}>

View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Actix web</title>
</head>
<body>
{{~#if let Some(name) = query.get("name") ~}}
<h1>Hi, {{ name }}!</h1>
{{~> alias/welcome tag='p' ~}}
{{~ else ~}}
{{~> alias/welcome tag="h1" ~}}
<p>
<h3>What is your name?</h3>
<form>
<input type="text" name="name"/><br/>
<p><input type="submit"></p>
</form>
</p>
{{~/if~}}
</body>
</html>

14
template_yarte/yarte.toml Normal file
View File

@ -0,0 +1,14 @@
# root dir of templates
[main]
dir = "templates"
debug = "code"
# Alias for partials. In call, change the start of partial path with one of this, if exist.
[partials]
alias = "./deep/more/deep"
[debug]
# prettyprint themes, put anything for options
theme = "zenburn"
number_line = true
grid = true