-
Notifications
You must be signed in to change notification settings - Fork 10
/
mre.rs
103 lines (86 loc) · 2.94 KB
/
mre.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use router::Router;
use request::Request;
use response::Response;
//use middleware::Middleware;
pub struct MRE<T: Copy> {
m2: @mongrel2::Connection,
router: router::Router<T>,
//middleware: ~[middleware<T>],
data: fn@() -> T,
}
/// Helper function to abstract away some of the boilerplate code.
pub fn MRE<T: Copy>(zmq: zmq::Context,
sender_id: Option<~str>,
req_addrs: ~[~str],
rep_addrs: ~[~str],
//middleware: ~[middleware<T>],
data: fn@() -> T) -> MRE<T> {
MRE {
m2: @mongrel2::connect(zmq, sender_id, req_addrs, rep_addrs),
router: router::Router(),
//middleware: copy middleware,
data: data
}
}
pub impl<T: Copy> MRE<T> {
fn run() {
io::println(fmt!("Starting up %? -> %?",
self.m2.req_addrs,
self.m2.rep_addrs));
loop {
let m2_req = match self.m2.recv() {
Ok(move req) => @req,
Err(e) => {
// Ignore invalid mongrel2 messages.
io::println(#fmt("warning: mongrel2 error: %s", e));
loop;
}
};
// Ignore close requests for now.
if m2_req.is_disconnect() { loop; }
let rep = @mut response::Response(self.m2, m2_req);
let req = match request::Request(m2_req, rep, self.data()) {
None => {
// Ignore this request if it's malformed.
loop;
}
Some(move req) => @req,
};
//if self.middleware.wrap(req, rep) {
// Only run the handler if the middleware hasn't handled
// the request.
match self.router.find(req.method, *req.path()) {
None => rep.reply_http(404u, ""),
Some((move handler, move m)) => handler(req, rep, m),
};
//}
}
}
fn head(regex: ~str, f: router::Handler<T>) {
self.router.add(request::HEAD, regex, f)
}
fn get(regex: ~str, f: router::Handler<T>) {
self.router.add(request::GET, regex, f)
}
fn post(regex: ~str, f: router::Handler<T>) {
self.router.add(request::POST, regex, f)
}
fn put(regex: ~str, f: router::Handler<T>) {
self.router.add(request::PUT, regex, f)
}
fn delete(regex: ~str, f: router::Handler<T>) {
self.router.add(request::DELETE, regex, f)
}
fn trace(regex: ~str, f: router::Handler<T>) {
self.router.add(request::TRACE, regex, f)
}
fn options(regex: ~str, f: router::Handler<T>) {
self.router.add(request::OPTIONS, regex, f)
}
fn connect(regex: ~str, f: router::Handler<T>) {
self.router.add(request::CONNECT, regex, f)
}
fn patch(regex: ~str, f: router::Handler<T>) {
self.router.add(request::PATCH, regex, f)
}
}