mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
feat: Example for Sailfish template engine (#528)
Co-authored-by: aaverichev <Aleksandr.Averichev@hermesrussia.ru> Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
parent
d815a44ccc
commit
ddb84ebf6f
135
Cargo.lock
generated
135
Cargo.lock
generated
@ -855,6 +855,35 @@ dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "actix-web-lab"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d2cd7a15518fb68eb657abc5b6b16c387f99b6db7f549f4884c72d05165139b8"
|
||||
dependencies = [
|
||||
"actix-files 0.6.0-beta.16",
|
||||
"actix-http 3.0.0-rc.3",
|
||||
"actix-service 2.0.2",
|
||||
"actix-utils 3.0.0",
|
||||
"actix-web 4.0.0-rc.3",
|
||||
"bytes 1.1.0",
|
||||
"csv",
|
||||
"derive_more",
|
||||
"digest 0.10.3",
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"hmac 0.12.1",
|
||||
"local-channel",
|
||||
"log",
|
||||
"mime",
|
||||
"once_cell",
|
||||
"pin-project-lite 0.2.8",
|
||||
"serde 1.0.136",
|
||||
"serde_json",
|
||||
"serde_urlencoded",
|
||||
"tokio 1.17.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "actix_derive"
|
||||
version = "0.5.0"
|
||||
@ -1546,7 +1575,10 @@ version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"serde 1.0.136",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2037,6 +2069,28 @@ dependencies = [
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "csv"
|
||||
version = "1.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1"
|
||||
dependencies = [
|
||||
"bstr",
|
||||
"csv-core",
|
||||
"itoa 0.4.8",
|
||||
"ryu",
|
||||
"serde 1.0.136",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "csv-core"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ctr"
|
||||
version = "0.8.0"
|
||||
@ -2422,6 +2476,18 @@ dependencies = [
|
||||
"instant",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "filetime"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "975ccf83d8d9d0d84682850a38c8169027be83368805971cc4f238c2b245bc98"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"winapi 0.3.9",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "firestorm"
|
||||
version = "0.5.0"
|
||||
@ -2867,6 +2933,15 @@ dependencies = [
|
||||
"digest 0.10.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "home"
|
||||
version = "0.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2456aef2e6b6a9784192ae780c0f15bc57df0e918585282325e8c8ac27737654"
|
||||
dependencies = [
|
||||
"winapi 0.3.9",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hostname"
|
||||
version = "0.3.1"
|
||||
@ -3162,6 +3237,12 @@ version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
|
||||
|
||||
[[package]]
|
||||
name = "itoap"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9028f49264629065d057f340a86acb84867925865f73bbf8d47b4d149a7e88b8"
|
||||
|
||||
[[package]]
|
||||
name = "jobserver"
|
||||
version = "0.1.24"
|
||||
@ -4744,6 +4825,12 @@ dependencies = [
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.25"
|
||||
@ -5120,6 +5207,43 @@ version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f"
|
||||
|
||||
[[package]]
|
||||
name = "sailfish"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "31f030905cfc5f869b6784dc4e5c83a20d1f77c92bae1a0d158baf9404100c3e"
|
||||
dependencies = [
|
||||
"itoap",
|
||||
"ryu",
|
||||
"sailfish-macros",
|
||||
"version_check 0.9.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sailfish-compiler"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7a8620f80b37af2e55153ee142bfdb8085f30c454be453e922466d483b0d184"
|
||||
dependencies = [
|
||||
"filetime",
|
||||
"home",
|
||||
"memchr",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"yaml-rust",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sailfish-macros"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b31ded532bded4637d49297e3247476ebc266efab4b054dc48037fa88846fdeb"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"sailfish-compiler",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "same-file"
|
||||
version = "1.0.6"
|
||||
@ -5876,6 +6000,17 @@ dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "template_sailfish"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"actix-web 4.0.0-rc.3",
|
||||
"actix-web-lab",
|
||||
"env_logger 0.9.0",
|
||||
"log",
|
||||
"sailfish",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "template_yarte"
|
||||
version = "1.0.0"
|
||||
|
@ -51,6 +51,7 @@ members = [
|
||||
"template_engines/tera",
|
||||
"template_engines/tinytemplate",
|
||||
"template_engines/yarte",
|
||||
"template_engines/sailfish",
|
||||
"websockets/autobahn",
|
||||
"websockets/chat-broker",
|
||||
"websockets/chat",
|
||||
|
@ -1,12 +1,16 @@
|
||||
# Handlebars
|
||||
|
||||
This is an example of how to use Actix Web with the [Handlebars templating language](https://crates.io/crates/handlebars), which is currently the most popular crate that achieves this. After starting the server with `cargo run`, you may visit the following pages:
|
||||
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
|
||||
|
11
template_engines/sailfish/Cargo.toml
Normal file
11
template_engines/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
template_engines/sailfish/README.md
Normal file
16
template_engines/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
template_engines/sailfish/src/main.rs
Normal file
62
template_engines/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
template_engines/sailfish/templates/actix.stpl
Normal file
1
template_engines/sailfish/templates/actix.stpl
Normal file
@ -0,0 +1 @@
|
||||
Hello user, <%= name %>!!!
|
1
template_engines/sailfish/templates/page.stpl
Normal file
1
template_engines/sailfish/templates/page.stpl
Normal file
@ -0,0 +1 @@
|
||||
Page id is <%= id %>!!!
|
Loading…
Reference in New Issue
Block a user