mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
restructure folders
This commit is contained in:
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 %>!!!
|
Reference in New Issue
Block a user