-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhttp_0mq.rb
62 lines (53 loc) · 1.48 KB
/
http_0mq.rb
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
# An example handler from the Mongrel2 manual. You can spin up many
# of these, Mongrel2 will then round-robin requests to each one.
#
# Running this example:
#
# m2sh load -config mongrel2.conf
# bundle exec foreman start
#
# Browse now to http://localhost:6767/handler to see the effect.
$stdout.sync = true
$stderr.sync = true
require 'm2r'
class Http0MQHandler < M2R::Handler
def on_wait
puts "WAITING FOR REQUEST"
end
def on_disconnect(request)
puts "DISCONNECT"
end
def on_error(request, response, error)
puts "ERROR:"
puts error.message
puts *error.backtrace
end
def process(request)
body = <<EOF
<pre>
SENDER: #{request.sender}
IDENT: #{request.conn_id}
PATH: #{request.path}
HEADERS: #{JSON.dump(request.headers.inject({}) {|hash,(h,v)| hash[h]=v; hash }, :pretty => true)}
PATTERN: #{request.pattern}
VERSION: #{request.http_version}
METHOD: #{request.method}
QUERY: #{request.query}
SCHEME: #{request.scheme}
BODY: #{request.body.inspect}
</pre>
EOF
response = M2R::Reply.new.to(request).body(body)
return response
end
end
sender_id = "34f9ceee-cd52-4b7f-b197-88bf2f0ec378"
pull_port = "tcp://127.0.0.1:9999"
pub_port = "tcp://127.0.0.1:9998"
factory = M2R::ConnectionFactory.new M2R::ConnectionFactory::Options.new(sender_id, pull_port, pub_port)
handler = Http0MQHandler.new(factory, M2R::Request)
graceful = Proc.new { handler.stop }
trap("INT", &graceful)
trap("TERM", &graceful)
handler.listen
M2R.zmq_context.terminate