| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#; |
| 2 | |
| 3 | use handlebarsrust-analyzer cargo handlebars 5.1.2 crate/::Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#; |
| 4 | use serde_jsonrust-analyzer cargo serde_json 1.0.114 crate/::jsonrust-analyzer cargo serde_json 1.0.114 macros/json!; |
| 5 | use tiny_httprust-analyzer cargo tiny_http 0.12.0 crate/::{Responserust-analyzer cargo tiny_http 0.12.0 response/Response#, Serverrust-analyzer cargo tiny_http 0.12.0 Server#}; |
| 6 | |
| 7 | fn handlebarsrust-analyzer cargo handlebars 5.1.2 handlebars().() -> Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#<'static> { |
| 8 | let mut reg = Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#::newrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]new().(); |
| 9 | // enable dev mode for template reloading |
| 10 | reg.set_dev_moderust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'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_filerust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_template_file().("tpl", "./examples/dev_mode/template.hbs") |
| 14 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 15 | |
| 16 | reg |
| 17 | } |
| 18 | |
| 19 | fn mainrust-analyzer cargo handlebars 5.1.2 main().() { |
| 20 | let hbs = Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T>`]new().(handlebarsrust-analyzer cargo handlebars 5.1.2 handlebars().()); |
| 21 | |
| 22 | let server = Serverrust-analyzer cargo tiny_http 0.12.0 Server#::httprust-analyzer cargo tiny_http 0.12.0 impl#[Server]http().("127.0.0.1:3030").expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Failed to start demo server."); |
| 23 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/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_requestsrust-analyzer cargo tiny_http 0.12.0 impl#[Server]incoming_requests().() { |
| 26 | let result = hbs |
| 27 | .renderrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render().("tpl", &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({"model": "t14s", "brand": "Thinkpad"})) |
| 28 | .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|e| e.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 29 | req.respondrust-analyzer cargo tiny_http 0.12.0 request/impl#[Request]respond().(Responserust-analyzer cargo tiny_http 0.12.0 response/Response#::from_stringrust-analyzer cargo tiny_http 0.12.0 response/impl#[`Response<Cursor<Vec<u8>>>`]from_string().(result)).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 30 | } |
| 31 | } |