Debian Codegraph @

examples/dev_mode.rs

raw symbols SCIP
1 use std::sync::Arc;
2
3 use handlebars::Handlebars;
4 use serde_json::json;
5 use tiny_http::{Response, Server};
6
7 fn handlebars() -> Handlebars<'static> {
8 let mut reg = Handlebars::new();
9 // enable dev mode for template reloading
10 reg.set_dev_mode(true);
11 // register a template from the file
12 // modified the file after the server starts to see things changing
13 reg.register_template_file("tpl", "./examples/dev_mode/template.hbs")
14 .unwrap();
15
16 reg
17 }
18
19 fn main() {
20 let hbs = Arc::new(handlebars());
21
22 let server = Server::http("127.0.0.1:3030").expect("Failed to start demo server.");
23 println!("Edit ./examples/dev_mode/template.hbs and request http://localhost:3030 to see the change on the fly.");
24
25 for req in server.incoming_requests() {
26 let result = hbs
27 .render("tpl", &json!({"model": "t14s", "brand": "Thinkpad"}))
28 .unwrap_or_else(|e| e.to_string());
29 req.respond(Response::from_string(result)).unwrap();
30 }
31 }