mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
restructure folders
This commit is contained in:
12
templating/askama/Cargo.toml
Normal file
12
templating/askama/Cargo.toml
Normal file
@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "template-askama"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
env_logger = "0.9.0"
|
||||
actix-web = "4.0.0-beta.21"
|
||||
askama = "0.11.0"
|
||||
|
||||
[build-dependencies]
|
||||
askama = "0.11.0"
|
45
templating/askama/src/main.rs
Normal file
45
templating/askama/src/main.rs
Normal file
@ -0,0 +1,45 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use actix_web::{middleware, web, App, HttpResponse, HttpServer, Result};
|
||||
use askama::Template;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "user.html")]
|
||||
struct UserTemplate<'a> {
|
||||
name: &'a str,
|
||||
text: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
struct Index;
|
||||
|
||||
async fn index(query: web::Query<HashMap<String, String>>) -> Result<HttpResponse> {
|
||||
let s = if let Some(name) = query.get("name") {
|
||||
UserTemplate {
|
||||
name,
|
||||
text: "Welcome!",
|
||||
}
|
||||
.render()
|
||||
.unwrap()
|
||||
} else {
|
||||
Index.render().unwrap()
|
||||
};
|
||||
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
||||
}
|
||||
|
||||
#[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(middleware::Logger::default())
|
||||
.service(web::resource("/").route(web::get().to(index)))
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
17
templating/askama/templates/index.html
Normal file
17
templating/askama/templates/index.html
Normal file
@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Actix Web</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Welcome!</h1>
|
||||
<p>
|
||||
<h3>What is your name?</h3>
|
||||
<form>
|
||||
<input type="text" name="name" /><br/>
|
||||
<p><input type="submit"></p>
|
||||
</form>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
13
templating/askama/templates/user.html
Normal file
13
templating/askama/templates/user.html
Normal file
@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Actix Web</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hi, {{ name }}!</h1>
|
||||
<p>
|
||||
{{ text }}
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
9
templating/handlebars/Cargo.toml
Normal file
9
templating/handlebars/Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "template_handlebars"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4.0.0-beta.21"
|
||||
handlebars = { version = "4.2.1", features = ["dir_source"] }
|
||||
serde_json = "1.0"
|
17
templating/handlebars/README.md
Normal file
17
templating/handlebars/README.md
Normal file
@ -0,0 +1,17 @@
|
||||
# Handlebars
|
||||
|
||||
This is an example of how to use Actix Web with the [Handlebars](https://crates.io/crates/handlebars) templating language, which is currently the most popular crate that achieves this.
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
cd template_engines/handlebars
|
||||
cargo run
|
||||
```
|
||||
|
||||
After starting the server, you may visit the following pages:
|
||||
|
||||
- http://localhost:8080
|
||||
- http://localhost:8080/Emma/documents
|
||||
- http://localhost:8080/Bob/passwords
|
||||
- http://localhost:8080/some-non-existing-page - 404 error rendered using template
|
109
templating/handlebars/src/main.rs
Normal file
109
templating/handlebars/src/main.rs
Normal file
@ -0,0 +1,109 @@
|
||||
use actix_web::body::BoxBody;
|
||||
use actix_web::dev::ServiceResponse;
|
||||
use actix_web::http::header::ContentType;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers};
|
||||
use actix_web::{get, web, App, HttpResponse, HttpServer, Result};
|
||||
use handlebars::Handlebars;
|
||||
use serde_json::json;
|
||||
use std::io;
|
||||
|
||||
// Macro documentation can be found in the actix_web_codegen crate
|
||||
#[get("/")]
|
||||
async fn index(hb: web::Data<Handlebars<'_>>) -> HttpResponse {
|
||||
let data = json!({
|
||||
"name": "Handlebars"
|
||||
});
|
||||
let body = hb.render("index", &data).unwrap();
|
||||
|
||||
HttpResponse::Ok().body(body)
|
||||
}
|
||||
|
||||
#[get("/{user}/{data}")]
|
||||
async fn user(
|
||||
hb: web::Data<Handlebars<'_>>,
|
||||
path: web::Path<(String, String)>,
|
||||
) -> HttpResponse {
|
||||
let info = path.into_inner();
|
||||
let data = json!({
|
||||
"user": info.0,
|
||||
"data": info.1
|
||||
});
|
||||
let body = hb.render("user", &data).unwrap();
|
||||
|
||||
HttpResponse::Ok().body(body)
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
// Handlebars uses a repository for the compiled templates. This object must be
|
||||
// shared between the application threads, and is therefore passed to the
|
||||
// Application Builder as an atomic reference-counted pointer.
|
||||
let mut handlebars = Handlebars::new();
|
||||
handlebars
|
||||
.register_templates_directory(".html", "./static/templates")
|
||||
.unwrap();
|
||||
let handlebars_ref = web::Data::new(handlebars);
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.wrap(error_handlers())
|
||||
.app_data(handlebars_ref.clone())
|
||||
.service(index)
|
||||
.service(user)
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
||||
// Custom error handlers, to return HTML responses when an error occurs.
|
||||
fn error_handlers() -> ErrorHandlers<BoxBody> {
|
||||
ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found)
|
||||
}
|
||||
|
||||
// Error handler for a 404 Page not found error.
|
||||
fn not_found<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<BoxBody>> {
|
||||
let response = get_error_response(&res, "Page not found");
|
||||
Ok(ErrorHandlerResponse::Response(ServiceResponse::new(
|
||||
res.into_parts().0,
|
||||
response.map_into_left_body(),
|
||||
)))
|
||||
}
|
||||
|
||||
// Generic error handler.
|
||||
fn get_error_response<B>(
|
||||
res: &ServiceResponse<B>,
|
||||
error: &str,
|
||||
) -> HttpResponse<BoxBody> {
|
||||
let request = res.request();
|
||||
|
||||
// Provide a fallback to a simple plain text response in case an error occurs during the
|
||||
// rendering of the error page.
|
||||
let fallback = |e: &str| {
|
||||
HttpResponse::build(res.status())
|
||||
.content_type(ContentType::plaintext())
|
||||
.body(e.to_string())
|
||||
};
|
||||
|
||||
let hb = request
|
||||
.app_data::<web::Data<Handlebars>>()
|
||||
.map(|t| t.get_ref());
|
||||
match hb {
|
||||
Some(hb) => {
|
||||
let data = json!({
|
||||
"error": error,
|
||||
"status_code": res.status().as_str()
|
||||
});
|
||||
let body = hb.render("error", &data);
|
||||
|
||||
match body {
|
||||
Ok(body) => HttpResponse::build(res.status())
|
||||
.content_type(ContentType::html())
|
||||
.body(body),
|
||||
Err(_) => fallback(error),
|
||||
}
|
||||
}
|
||||
None => fallback(error),
|
||||
}
|
||||
}
|
10
templating/handlebars/static/templates/error.html
Normal file
10
templating/handlebars/static/templates/error.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>{{error}}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{status_code}} {{error}}</h1>
|
||||
</body>
|
||||
</html>
|
12
templating/handlebars/static/templates/index.html
Normal file
12
templating/handlebars/static/templates/index.html
Normal file
@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>{{name}} Example</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>{{name}} example</h1>
|
||||
<p>This is an example of how to use {{name}} with Actix-Web.</p>
|
||||
</body>
|
||||
</html>
|
12
templating/handlebars/static/templates/user.html
Normal file
12
templating/handlebars/static/templates/user.html
Normal file
@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>{{user}}'s homepage</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Welcome back, {{user}}</h1>
|
||||
<p>Here's your {{data}}.</p>
|
||||
</body>
|
||||
</html>
|
11
templating/sailfish/Cargo.toml
Normal file
11
templating/sailfish/Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "template_sailfish"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4.0.0-rc.3"
|
||||
actix-web-lab = "0.10"
|
||||
env_logger = "0.9"
|
||||
log = "0.4"
|
||||
sailfish = "0.3"
|
16
templating/sailfish/README.md
Normal file
16
templating/sailfish/README.md
Normal file
@ -0,0 +1,16 @@
|
||||
# Sailfish
|
||||
|
||||
This is an example of how to use Actix Web with the [Sailfish](https://sailfish.netlify.app/) templating language.
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
cd template_engines/sailfish
|
||||
cargo run
|
||||
```
|
||||
|
||||
After starting the server, you may visit the following pages:
|
||||
|
||||
- http://localhost:8080/page-1
|
||||
- http://localhost:8080/page-678
|
||||
- http://localhost:8080/Username
|
62
templating/sailfish/src/main.rs
Normal file
62
templating/sailfish/src/main.rs
Normal file
@ -0,0 +1,62 @@
|
||||
use actix_web::{
|
||||
error, get,
|
||||
middleware::{Compress, Logger},
|
||||
web, App, HttpServer, Responder,
|
||||
};
|
||||
use actix_web_lab::respond::Html;
|
||||
use sailfish::TemplateOnce;
|
||||
|
||||
#[derive(TemplateOnce)]
|
||||
#[template(path = "actix.stpl")]
|
||||
struct Greet<'a> {
|
||||
name: &'a str,
|
||||
}
|
||||
|
||||
#[derive(TemplateOnce)]
|
||||
#[template(path = "page.stpl")]
|
||||
struct Page<'a> {
|
||||
id: &'a i32,
|
||||
}
|
||||
|
||||
#[get("/{name}")]
|
||||
async fn greet(params: web::Path<(String,)>) -> actix_web::Result<impl Responder> {
|
||||
let body = Greet { name: ¶ms.0 }
|
||||
.render_once()
|
||||
.map_err(error::ErrorInternalServerError)?;
|
||||
|
||||
Ok(Html(body))
|
||||
}
|
||||
|
||||
#[get("/page-{id:\\d+}")]
|
||||
async fn page(params: web::Path<(i32,)>) -> actix_web::Result<impl Responder> {
|
||||
let body = Page { id: ¶ms.0 }
|
||||
.render_once()
|
||||
.map_err(error::ErrorInternalServerError)?;
|
||||
|
||||
Ok(Html(body))
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn hello() -> impl Responder {
|
||||
Html("<p>Hello world!</p>".to_string())
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
log::info!("starting HTTP server at http://localhost:8080");
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.service(hello)
|
||||
.service(page)
|
||||
.service(greet)
|
||||
.wrap(Compress::default())
|
||||
.wrap(Logger::default())
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.workers(1)
|
||||
.run()
|
||||
.await
|
||||
}
|
1
templating/sailfish/templates/actix.stpl
Normal file
1
templating/sailfish/templates/actix.stpl
Normal file
@ -0,0 +1 @@
|
||||
Hello user, <%= name %>!!!
|
1
templating/sailfish/templates/page.stpl
Normal file
1
templating/sailfish/templates/page.stpl
Normal file
@ -0,0 +1 @@
|
||||
Page id is <%= id %>!!!
|
9
templating/tera/Cargo.toml
Normal file
9
templating/tera/Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "template-tera"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
env_logger = "0.9.0"
|
||||
tera = "1.8.0"
|
||||
actix-web = "4.0.0-beta.21"
|
18
templating/tera/README.md
Normal file
18
templating/tera/README.md
Normal file
@ -0,0 +1,18 @@
|
||||
# template_tera
|
||||
|
||||
Minimal example of using the template [tera](https://github.com/Keats/tera) that displays a form.
|
||||
|
||||
## Usage
|
||||
|
||||
### server
|
||||
|
||||
```bash
|
||||
cd template_engines/tera
|
||||
cargo run (or ``cargo watch -x run``)
|
||||
# Started http server: 127.0.0.1:8080
|
||||
```
|
||||
|
||||
### web client
|
||||
|
||||
- [http://localhost:8080](http://localhost:8080)
|
||||
- [http://localhost:8080/non-existing-page](http://localhost:8080/non-existing-page) - 404 page rendered using template
|
93
templating/tera/src/main.rs
Normal file
93
templating/tera/src/main.rs
Normal file
@ -0,0 +1,93 @@
|
||||
use actix_web::body::BoxBody;
|
||||
use actix_web::dev::ServiceResponse;
|
||||
use actix_web::http::header::ContentType;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers};
|
||||
use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer, Result};
|
||||
use std::collections::HashMap;
|
||||
use tera::Tera;
|
||||
|
||||
// store tera template in application state
|
||||
async fn index(
|
||||
tmpl: web::Data<tera::Tera>,
|
||||
query: web::Query<HashMap<String, String>>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let s = if let Some(name) = query.get("name") {
|
||||
// submitted form
|
||||
let mut ctx = tera::Context::new();
|
||||
ctx.insert("name", &name.to_owned());
|
||||
ctx.insert("text", &"Welcome!".to_owned());
|
||||
tmpl.render("user.html", &ctx)
|
||||
.map_err(|_| error::ErrorInternalServerError("Template error"))?
|
||||
} else {
|
||||
tmpl.render("index.html", &tera::Context::new())
|
||||
.map_err(|_| error::ErrorInternalServerError("Template error"))?
|
||||
};
|
||||
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
env_logger::init();
|
||||
|
||||
println!("Listening on: 127.0.0.1:8080, open browser and visit have a try!");
|
||||
HttpServer::new(|| {
|
||||
let tera =
|
||||
Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")).unwrap();
|
||||
|
||||
App::new()
|
||||
.app_data(web::Data::new(tera))
|
||||
.wrap(middleware::Logger::default()) // enable logger
|
||||
.service(web::resource("/").route(web::get().to(index)))
|
||||
.service(web::scope("").wrap(error_handlers()))
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
||||
// Custom error handlers, to return HTML responses when an error occurs.
|
||||
fn error_handlers() -> ErrorHandlers<BoxBody> {
|
||||
ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found)
|
||||
}
|
||||
|
||||
// Error handler for a 404 Page not found error.
|
||||
fn not_found<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<BoxBody>> {
|
||||
let response = get_error_response(&res, "Page not found");
|
||||
Ok(ErrorHandlerResponse::Response(ServiceResponse::new(
|
||||
res.into_parts().0,
|
||||
response.map_into_left_body(),
|
||||
)))
|
||||
}
|
||||
|
||||
// Generic error handler.
|
||||
fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> HttpResponse {
|
||||
let request = res.request();
|
||||
|
||||
// Provide a fallback to a simple plain text response in case an error occurs during the
|
||||
// rendering of the error page.
|
||||
let fallback = |e: &str| {
|
||||
HttpResponse::build(res.status())
|
||||
.content_type(ContentType::plaintext())
|
||||
.body(e.to_string())
|
||||
};
|
||||
|
||||
let tera = request.app_data::<web::Data<Tera>>().map(|t| t.get_ref());
|
||||
match tera {
|
||||
Some(tera) => {
|
||||
let mut context = tera::Context::new();
|
||||
context.insert("error", error);
|
||||
context.insert("status_code", res.status().as_str());
|
||||
let body = tera.render("error.html", &context);
|
||||
|
||||
match body {
|
||||
Ok(body) => HttpResponse::build(res.status())
|
||||
.content_type(ContentType::html())
|
||||
.body(body),
|
||||
Err(_) => fallback(error),
|
||||
}
|
||||
}
|
||||
None => fallback(error),
|
||||
}
|
||||
}
|
10
templating/tera/templates/error.html
Normal file
10
templating/tera/templates/error.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>{{ error }}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{ status_code }} {{ error }}</h1>
|
||||
</body>
|
||||
</html>
|
17
templating/tera/templates/index.html
Normal file
17
templating/tera/templates/index.html
Normal file
@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Actix Web</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Welcome!</h1>
|
||||
<p>
|
||||
<h3>What is your name?</h3>
|
||||
<form>
|
||||
<input type="text" name="name" /><br/>
|
||||
<p><input type="submit"></p>
|
||||
</form>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
13
templating/tera/templates/user.html
Normal file
13
templating/tera/templates/user.html
Normal file
@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Actix Web</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hi, {{ name }}!</h1>
|
||||
<p>
|
||||
{{ text }}
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
10
templating/tinytemplate/Cargo.toml
Normal file
10
templating/tinytemplate/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "template-tinytemplate"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
env_logger = "0.9.0"
|
||||
tinytemplate = "1.1"
|
||||
actix-web = "4.0.0-beta.21"
|
||||
serde_json = "1"
|
18
templating/tinytemplate/README.md
Normal file
18
templating/tinytemplate/README.md
Normal file
@ -0,0 +1,18 @@
|
||||
# template_tinytemplate
|
||||
|
||||
See the documentation for the minimalist template engine [tiny_template](https://docs.rs/tinytemplate/1.1.0/tinytemplate/).
|
||||
|
||||
## Usage
|
||||
|
||||
### Server
|
||||
|
||||
```bash
|
||||
cd template_engines/tinytemplate
|
||||
cargo run # (or ``cargo watch -x run``)
|
||||
# Started http server: 127.0.0.1:8080
|
||||
```
|
||||
|
||||
### Web Client
|
||||
|
||||
- [http://localhost:8080](http://localhost:8080)
|
||||
- [http://localhost:8080/non-existing-page](http://localhost:8080/non-existing-page) - 404 page rendered using template
|
103
templating/tinytemplate/src/main.rs
Normal file
103
templating/tinytemplate/src/main.rs
Normal file
@ -0,0 +1,103 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use actix_web::body::BoxBody;
|
||||
use actix_web::dev::ServiceResponse;
|
||||
use actix_web::http::header::ContentType;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers};
|
||||
use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer, Result};
|
||||
use serde_json::json;
|
||||
use tinytemplate::TinyTemplate;
|
||||
|
||||
// store tiny_template in application state
|
||||
async fn index(
|
||||
tmpl: web::Data<TinyTemplate<'_>>,
|
||||
query: web::Query<HashMap<String, String>>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let s = if let Some(name) = query.get("name") {
|
||||
// submitted form
|
||||
let ctx = json!({
|
||||
"name" : name.to_owned(),
|
||||
"text" : "Welcome!".to_owned()
|
||||
});
|
||||
tmpl.render("user.html", &ctx)
|
||||
.map_err(|_| error::ErrorInternalServerError("Template error"))?
|
||||
} else {
|
||||
tmpl.render("index.html", &serde_json::Value::Null)
|
||||
.map_err(|_| error::ErrorInternalServerError("Template error"))?
|
||||
};
|
||||
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
env_logger::init();
|
||||
|
||||
HttpServer::new(|| {
|
||||
let mut tt = TinyTemplate::new();
|
||||
tt.add_template("index.html", INDEX).unwrap();
|
||||
tt.add_template("user.html", USER).unwrap();
|
||||
tt.add_template("error.html", ERROR).unwrap();
|
||||
|
||||
App::new()
|
||||
.app_data(web::Data::new(tt))
|
||||
.wrap(middleware::Logger::default()) // enable logger
|
||||
.service(web::resource("/").route(web::get().to(index)))
|
||||
.service(web::scope("").wrap(error_handlers()))
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
||||
// Custom error handlers, to return HTML responses when an error occurs.
|
||||
fn error_handlers() -> ErrorHandlers<BoxBody> {
|
||||
ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found)
|
||||
}
|
||||
|
||||
// Error handler for a 404 Page not found error.
|
||||
fn not_found<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<BoxBody>> {
|
||||
let response = get_error_response(&res, "Page not found");
|
||||
Ok(ErrorHandlerResponse::Response(ServiceResponse::new(
|
||||
res.into_parts().0,
|
||||
response.map_into_left_body(),
|
||||
)))
|
||||
}
|
||||
|
||||
// Generic error handler.
|
||||
fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> HttpResponse {
|
||||
let request = res.request();
|
||||
|
||||
// Provide a fallback to a simple plain text response in case an error occurs during the
|
||||
// rendering of the error page.
|
||||
let fallback = |e: &str| {
|
||||
HttpResponse::build(res.status())
|
||||
.content_type(ContentType::plaintext())
|
||||
.body(e.to_string())
|
||||
};
|
||||
|
||||
let tt = request
|
||||
.app_data::<web::Data<TinyTemplate<'_>>>()
|
||||
.map(|t| t.get_ref());
|
||||
match tt {
|
||||
Some(tt) => {
|
||||
let mut context = std::collections::HashMap::new();
|
||||
context.insert("error", error.to_owned());
|
||||
context.insert("status_code", res.status().as_str().to_owned());
|
||||
let body = tt.render("error.html", &context);
|
||||
|
||||
match body {
|
||||
Ok(body) => HttpResponse::build(res.status())
|
||||
.content_type(ContentType::html())
|
||||
.body(body),
|
||||
Err(_) => fallback(error),
|
||||
}
|
||||
}
|
||||
None => fallback(error),
|
||||
}
|
||||
}
|
||||
|
||||
static ERROR: &str = include_str!("../templates/error.html");
|
||||
static INDEX: &str = include_str!("../templates/index.html");
|
||||
static USER: &str = include_str!("../templates/user.html");
|
10
templating/tinytemplate/templates/error.html
Normal file
10
templating/tinytemplate/templates/error.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>{error}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{status_code} {error}</h1>
|
||||
</body>
|
||||
</html>
|
17
templating/tinytemplate/templates/index.html
Normal file
17
templating/tinytemplate/templates/index.html
Normal file
@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Actix Web</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Welcome!</h1>
|
||||
<p>
|
||||
<h3>What is your name?</h3>
|
||||
<form>
|
||||
<input type="text" name="name" /><br/>
|
||||
<p><input type="submit"></p>
|
||||
</form>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
11
templating/tinytemplate/templates/user.html
Normal file
11
templating/tinytemplate/templates/user.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Actix Web</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hi, {name}!</h1>
|
||||
<p>{text}</p>
|
||||
</body>
|
||||
</html>
|
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