#![deny(warnings)] use std::sync::Arc; use handlebars::Handlebars; use serde::Serialize; use serde_json::json; use warp::Filter; struct WithTemplate { name: &'static str, value: T, } fn render(template: WithTemplate, hbs: Arc>) -> impl warp::Reply where T: Serialize, { let render = hbs .render(template.name, &template.value) .unwrap_or_else(|err| err.to_string()); warp::reply::html(render) } #[tokio::main] async fn main() { let template = " Warp Handlebars template example

Hello {{user}}!

"; let mut hb = Handlebars::new(); // register the template hb.register_template_string("template.html", template) .unwrap(); // Turn Handlebars instance into a Filter so we can combine it // easily with others... let hb = Arc::new(hb); // Create a reusable closure to render template let handlebars = move |with_template| render(with_template, hb.clone()); //GET / let route = warp::get() .and(warp::path::end()) .map(|| WithTemplate { name: "template.html", value: json!({"user" : "Warp"}), }) .map(handlebars); warp::serve(route).run(([127, 0, 0, 1], 3030)).await; }