diff --git a/Cargo.lock b/Cargo.lock index 014b5425..978a7f74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index b14e25ef..db90ab60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,6 +51,7 @@ members = [ "template_engines/tera", "template_engines/tinytemplate", "template_engines/yarte", + "template_engines/sailfish", "websockets/autobahn", "websockets/chat-broker", "websockets/chat", diff --git a/template_engines/handlebars/README.md b/template_engines/handlebars/README.md index e7061a60..78bac4bd 100644 --- a/template_engines/handlebars/README.md +++ b/template_engines/handlebars/README.md @@ -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 diff --git a/template_engines/handlebars/static/templates/index.html b/template_engines/handlebars/static/templates/index.html index 867c114e..013026cb 100644 --- a/template_engines/handlebars/static/templates/index.html +++ b/template_engines/handlebars/static/templates/index.html @@ -9,4 +9,4 @@

{{name}} example

This is an example of how to use {{name}} with Actix-Web.

- \ No newline at end of file + diff --git a/template_engines/handlebars/static/templates/user.html b/template_engines/handlebars/static/templates/user.html index 59e2f1fa..be7c765e 100644 --- a/template_engines/handlebars/static/templates/user.html +++ b/template_engines/handlebars/static/templates/user.html @@ -9,4 +9,4 @@

Welcome back, {{user}}

Here's your {{data}}.

- \ No newline at end of file + diff --git a/template_engines/sailfish/Cargo.toml b/template_engines/sailfish/Cargo.toml new file mode 100644 index 00000000..8b2e2e83 --- /dev/null +++ b/template_engines/sailfish/Cargo.toml @@ -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" diff --git a/template_engines/sailfish/README.md b/template_engines/sailfish/README.md new file mode 100644 index 00000000..fe67ada4 --- /dev/null +++ b/template_engines/sailfish/README.md @@ -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 diff --git a/template_engines/sailfish/src/main.rs b/template_engines/sailfish/src/main.rs new file mode 100644 index 00000000..4ab9c2c5 --- /dev/null +++ b/template_engines/sailfish/src/main.rs @@ -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 { + 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 { + let body = Page { id: ¶ms.0 } + .render_once() + .map_err(error::ErrorInternalServerError)?; + + Ok(Html(body)) +} + +#[get("/")] +async fn hello() -> impl Responder { + Html("

Hello world!

".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 +} diff --git a/template_engines/sailfish/templates/actix.stpl b/template_engines/sailfish/templates/actix.stpl new file mode 100644 index 00000000..20a76bc8 --- /dev/null +++ b/template_engines/sailfish/templates/actix.stpl @@ -0,0 +1 @@ +Hello user, <%= name %>!!! diff --git a/template_engines/sailfish/templates/page.stpl b/template_engines/sailfish/templates/page.stpl new file mode 100644 index 00000000..cce1b92a --- /dev/null +++ b/template_engines/sailfish/templates/page.stpl @@ -0,0 +1 @@ +Page id is <%= id %>!!!