-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathhtml_handlebars.rs
89 lines (81 loc) · 2.9 KB
/
html_handlebars.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/// Web service that receives and responds with HTML.
///
/// ## Overview
///
/// Tower web supports templates and responding with HTML using the handlebars
/// templating engine. Plain old Rust structs are used to represent data and
/// are used as the handler return value. Tower Web passes the response structs
/// to the handlebars serializer. HTML is rendered using a handlebars template
/// and populated using the data in the response struct.
///
/// ## Usage
///
/// Run the example:
///
/// cargo run --example html_handlebars
///
/// Then send a request:
///
/// curl -v http://localhost:8080/
extern crate env_logger;
#[macro_use]
extern crate tower_web;
use tower_web::ServiceBuilder;
use tower_web::view::Handlebars;
/// This type will be the web service implementation.
#[derive(Clone, Debug)]
struct HtmlResource;
/// The type is annotated with `#[derive(Response)]`, this allows `MyResponse`
/// to be used as a response to resource methods.
///
/// We are using the handlebars serializer to render the HTML response. It
/// requires that a template to render is specified. This is done with the
/// `#[web(template = "<template name>")]` attribute.
///
/// The default location to look for templates is in the `templates` directory
/// at the crate root. To make the example work, this crate has a handlebars
/// template at "examples/hello_world.hbs" in the templates directory at the
/// crate root.
#[derive(Debug, Response)]
struct MyResponse {
title: &'static str,
}
impl_web! {
impl HtmlResource {
// Respond as HTML. For this to work, a serializer supporting HTML must
// be added to the service.
//
// If no serializer is specified, a 500 response will be returned.
//
#[get("/")]
#[content_type("html")]
#[web(template = "examples/hello_world")]
fn hello_world(&self) -> Result<MyResponse, ()> {
Ok(MyResponse {
title: "Handler variable",
})
}
}
}
pub fn main() {
let _ = env_logger::try_init();
let addr = "127.0.0.1:8080".parse().expect("Invalid address");
println!("Listening on http://{}", addr);
ServiceBuilder::new()
.resource(HtmlResource)
// Add the handlebars serializer to the application. This uses the
// template rendering default settings. Templates are located at
// the crate root in the `templates` directory. Template files
// use the `.hbs` extension.
//
// The handlebars serializer is configured by calling
// `Handlebars::new_with_registry` and passing in a configured
// registry. This allows changing the template directory as well
// as defining helpers and other configuration options.
//
// See the `handlebars` crate for more documentation on configuration
// options.
.serializer(Handlebars::new())
.run(&addr)
.unwrap();
}