@@ -10,12 +10,12 @@ It is basically a HTTP toolkit for building HTTP-friendly applications using the
10
10
Webmachine-rust works with Hyper and sits between the Hyper Handler and your application code. It provides a resource struct
11
11
with callbacks to handle the decisions required as the state machine is executed against the request with the following sequence.
12
12
13
- REQUEST -> Hyper Handler -> WebmachineDispatcher -> WebmachineResource -> Your application code -> WebmachineResponse -> Hyper -> RESPONSE
13
+ REQUEST -> Hyper Handler -> WebmachineDispatcher -> Resource -> Your application code -> WebmachineResponse -> Hyper -> RESPONSE
14
14
15
15
## Features
16
16
17
17
- Handles the hard parts of content negotiation, conditional requests, and response codes for you.
18
- - Provides a resource struct with points of extension to let you describe what is relevant about your particular resource.
18
+ - Provides a resource trait and struct with points of extension to let you describe what is relevant about your particular resource.
19
19
20
20
## Missing Features
21
21
@@ -30,14 +30,19 @@ This implementation has the following deficiencies:
30
30
31
31
- Automatically decoding request bodies and encoding response bodies.
32
32
- No easy mechanism to generate bodies with different content types (e.g. JSON vs. XML).
33
- - No easy mechanism for handling sub-paths in a resource.
34
33
- Dynamically determining the methods allowed on the resource.
35
34
36
35
## Getting started with Hyper
37
36
38
37
Follow the getting started documentation from the Hyper crate to setup a Hyper service for your server.
39
- You need to define a WebmachineDispatcher that maps resource paths to your webmachine resources (WebmachineResource).
40
- Each WebmachineResource defines all the callbacks (via Closures) and values required to implement a resource.
38
+
39
+ There are two ways of using this crate. You can either use the `WebmachineResource` struct and add callbacks
40
+ for the state you need to modify, or you can create your own resource structs and implement the
41
+ `Resource` trait.
42
+
43
+ You need to define a WebmachineDispatcher that maps resource paths to your webmachine resources
44
+ (WebmachineResource or structs that implement Resource). WebmachineResource defines all the callbacks
45
+ (via Closures) and values required to implement a resource.
41
46
42
47
Note: This example uses the maplit crate to provide the `btreemap` macro and the log crate for the logging macros.
43
48
@@ -70,7 +75,7 @@ Note: This example uses the maplit crate to provide the `btreemap` macro and the
70
75
// use it in the loop below.
71
76
let dispatcher = Arc::new(WebmachineDispatcher {
72
77
routes: btreemap!{
73
- "/myresource" => WebmachineResource {
78
+ "/myresource" => WebmachineDispatcher::box_resource( WebmachineResource {
74
79
// Methods allowed on this resource
75
80
allowed_methods: owned_vec(&["OPTIONS", "GET", "HEAD", "POST"]),
76
81
// if the resource exists callback
@@ -86,7 +91,7 @@ Note: This example uses the maplit crate to provide the `btreemap` macro and the
86
91
process_post: async_callback(|_, _| /* Handle the post here */ ready(Ok(true)).boxed() ),
87
92
// default everything else
88
93
.. WebmachineResource::default()
89
- }
94
+ })
90
95
}
91
96
});
92
97
@@ -1495,7 +1500,7 @@ fn generate_http_response(context: &WebmachineContext) -> http::Result<Response<
1495
1500
/// The main hyper dispatcher
1496
1501
pub struct WebmachineDispatcher {
1497
1502
/// Map of routes to webmachine resources
1498
- pub routes : BTreeMap < & ' static str , WebmachineResource >
1503
+ pub routes : BTreeMap < & ' static str , Box < dyn Resource + Send + Sync > >
1499
1504
}
1500
1505
1501
1506
impl WebmachineDispatcher {
@@ -1524,8 +1529,9 @@ impl WebmachineDispatcher {
1524
1529
. collect ( )
1525
1530
}
1526
1531
1527
- fn lookup_resource ( & self , path : & str ) -> Option < & WebmachineResource > {
1532
+ fn lookup_resource ( & self , path : & str ) -> Option < & ( dyn Resource + Send + Sync ) > {
1528
1533
self . routes . get ( path)
1534
+ . map ( |resource| resource. as_ref ( ) )
1529
1535
}
1530
1536
1531
1537
/// Dispatches to the matching webmachine resource. If there is no matching resource, returns
@@ -1549,6 +1555,11 @@ impl WebmachineDispatcher {
1549
1555
None => context. response . status = 404
1550
1556
} ;
1551
1557
}
1558
+
1559
+ /// Convenience function to box a resource
1560
+ pub fn box_resource < R : Resource + Send + Sync + ' static > ( resource : R ) -> Box < dyn Resource + Send + Sync > {
1561
+ Box :: new ( resource)
1562
+ }
1552
1563
}
1553
1564
1554
1565
#[ cfg( test) ]
0 commit comments