Add actix-web example

[skip ci]
This commit is contained in:
Kogia-sima 2020-06-07 05:32:35 +09:00
parent 3a9260f2a7
commit f0cd89a949
4 changed files with 1360 additions and 0 deletions

1312
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,8 @@ edition = "2018"
publish = false
[dependencies]
actix-web = { version = "2.0.0", default-features = false }
actix-rt = "1.1.1"
sailfish = { path = "../sailfish" }
sailfish-macros = { path = "../sailfish-macros" }
@ -18,3 +20,8 @@ test = false
name = "include"
path = "include.rs"
test = false
[[bin]]
name = "actix"
path = "actix.rs"
test = false

36
examples/actix.rs Normal file
View File

@ -0,0 +1,36 @@
#[macro_use]
extern crate sailfish_macros;
use actix_web::error::InternalError;
use actix_web::http::StatusCode;
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer};
use sailfish::TemplateOnce;
#[derive(TemplateOnce)]
#[template(path = "actix.stpl")]
struct Greet<'a> {
name: &'a str,
}
async fn greet(req: HttpRequest) -> actix_web::Result<HttpResponse> {
let name = req.match_info().get("name").unwrap_or("World");
let body = Greet { name }
.render_once()
.map_err(|e| InternalError::new(e, StatusCode::INTERNAL_SERVER_ERROR))?;
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(body))
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.route("/", web::get().to(greet))
.route("/{name}", web::get().to(greet))
})
.bind("127.0.0.1:8000")?
.run()
.await
}

View File

@ -0,0 +1,5 @@
<html>
<body>
Hello <%= name %>!
</body>
</html>