1
0
mirror of https://github.com/actix/examples synced 2025-06-29 10:14:58 +02:00

Add template yarte

This commit is contained in:
Juan Aguilar Santillana
2019-03-12 21:15:53 +01:00
parent 14eed91fcd
commit 7ed8d19b66
10 changed files with 112 additions and 0 deletions

21
template_yarte/src/lib.rs Normal file
View File

@ -0,0 +1,21 @@
use actix_web::{error::ErrorInternalServerError, web::Query, HttpResponse, Result};
use yarte::Template;
use std::collections::HashMap;
#[derive(Template)]
#[template(path = "index.hbs")]
struct IndexTemplate {
query: Query<HashMap<String, String>>,
}
pub fn index(query: Query<HashMap<String, String>>) -> Result<HttpResponse> {
IndexTemplate { query }
.call()
.map(|s| {
HttpResponse::Ok()
.content_type(IndexTemplate::mime())
.body(s)
})
.map_err(|_| ErrorInternalServerError("Template parsing error"))
}

View File

@ -0,0 +1,19 @@
use actix_web::{middleware, web, App, HttpServer};
#[path = "lib.rs"]
mod template;
fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
// start http server
HttpServer::new(|| {
App::new()
// enable logger
.middleware(middleware::Logger::default())
.service(web::resource("/").to(template::index))
})
.bind("127.0.0.1:8080")?
.run()
}