Skip to content

Commit adbc14e

Browse files
committed
Revise examples and provide starting guide.
1 parent d7b41a9 commit adbc14e

File tree

6 files changed

+91
-52
lines changed

6 files changed

+91
-52
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ spec/reports
1515
test/tmp
1616
test/version_tmp
1717
tmp
18+
example/tmp
19+
example/config.sqlite

example/http_0mq.rb

+30-19
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
# http_0mq.rb - an example handler from the Mongrel2 book
2-
# You can spin up many of these - Mongrel2 will then round-robin requests to each one.
1+
# An example handler from the Mongrel2 manual. You can spin up many
2+
# of these, Mongrel2 will then round-robin requests to each one.
3+
#
4+
# Running this example:
5+
#
6+
# bundle exec ruby -I../lib http_0mq.rb
7+
#
8+
# m2sh load -config mongrel2.conf
9+
# m2sh start -name main
10+
#
11+
# curl http://localhost:6767
312

4-
# require 'rubygems'
5-
# require 'ruby-debug'
6-
# Debugger.start
7-
$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
8-
require 'm2r'
9-
10-
class Http0MQHandler < Mongrel2::Handler
11-
# There are more hooks you can override - check out lib/handler.rb
13+
require 'm2r/handler'
14+
require 'securerandom'
1215

16+
class Http0MQHandler < M2R::Handler
1317
def on_wait
1418
puts "WAITING FOR REQUEST"
1519
end
@@ -18,16 +22,23 @@ def on_disconnect
1822
puts "DISCONNECT"
1923
end
2024

21-
def process(req)
22-
response = "<pre>\nSENDER: %s\nIDENT:%s\nPATH: %s\nHEADERS:%s\nBODY:%s</pre>" % [
23-
req.sender.inspect, req.conn_id.inspect, req.path.inspect,
24-
JSON.generate(req.headers).inspect, req.body.inspect]
25-
puts response
26-
response
25+
def process(request)
26+
<<EOF
27+
<pre>
28+
SENDER: #{request.sender}
29+
IDENT: #{request.conn_id}
30+
PATH: #{request.path}
31+
HEADERS: #{JSON.pretty_generate(request.headers)}
32+
BODY: #{request.body.inspect}
33+
</pre>
34+
EOF
2735
end
2836
end
2937

30-
sender_id = "C2256F34-14A1-45DD-BB73-97CAE25E25B4"
31-
handler = Http0MQHandler.new(
32-
sender_id, "tcp://127.0.0.1:9997", "tcp://127.0.0.1:9996")
38+
sender_id = SecureRandom.uuid
39+
pull_port = "tcp://127.0.0.1:9997"
40+
pub_port = "tcp://127.0.0.1:9996"
41+
42+
handler = Http0MQHandler.new(sender_id, pull_port, pub_port)
3343
handler.listen
44+

example/lobster.ru

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
# An example of running Rack application.
2+
#
3+
# Running this example:
4+
#
5+
# bundle exec rackup -I../lib lobster.ru
6+
#
7+
# m2sh load -config mongrel2.conf
8+
# m2sh start -name main
9+
#
10+
# curl http://localhost:6767
11+
112
require 'rack/lobster'
2-
$: << ::File.dirname(__FILE__)
3-
require 'rack_handler'
13+
require './rack_handler'
414

515
use Rack::ShowExceptions
6-
puts "Lobster at http://localhost:6767/handlertest"
7-
Rack::Handler::Mongrel2.run Rack::Lobster.new
16+
Rack::Handler::Mongrel2.run Rack::Lobster.new
17+

example/mongrel2.conf

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
handler_test = Handler(
2+
send_spec = "tcp://127.0.0.1:9997",
3+
send_ident = "34f9ceee-cd52-4b7f-b197-88bf2f0ec378",
4+
recv_spec = "tcp://127.0.0.1:9996",
5+
recv_ident = ""
6+
)
7+
8+
mongrel2 = Host(
9+
name = "127.0.0.1",
10+
routes = { "/": handler_test }
11+
)
12+
13+
main = Server(
14+
uuid="2f62bd5-9e59-49cd-993c-3b6013c28f05",
15+
access_log = "/tmp/access.log",
16+
error_log = "/tmp/error.log",
17+
chroot = "./",
18+
pid_file = "/tmp/mongrel2.pid",
19+
default_host = "127.0.0.1",
20+
name = "main",
21+
port = 6767,
22+
hosts = [mongrel2]
23+
)
24+
25+
servers = [main]
26+

example/rack_handler.rb

+18-29
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
1-
require 'rubygems'
1+
require 'm2r/connection'
22
require 'rack'
33
require 'stringio'
4-
# require 'ruby-debug'
5-
# Debugger.start
6-
# gem install ruby-debug19 -- --with-ruby-include=$HOME/.rvm/src/ruby-1.9.2-head
7-
8-
$: << ::File.expand_path(::File.dirname(__FILE__) + '/../lib')
9-
require 'connection'
10-
11-
$sender_id = "70D107AB-19F5-44AE-A2D0-2326A167D8D7"
4+
require 'securerandom'
125

136
module Rack
147
module Handler
158
class Mongrel2
169
def self.run(app, receive = "tcp://127.0.0.1:9997", send = "tcp://127.0.0.1:9996")
17-
conn = ::Mongrel2::Connection.new($sender_id, receive, send)
18-
@running = true
19-
trap("SIGINT") do
20-
@running = false
21-
end
10+
connection = M2R::Connection.new(SecureRandom.uuid, receive, send)
11+
@running = true
12+
trap("SIGINT") { @running = false }
2213

2314
while @running
2415
puts "WAITING FOR REQUEST"
2516

26-
req = conn.recv # Caution: Abort traps on SIGINT :/
17+
req = connection.recv
2718
if req.disconnect?
2819
puts "DICONNECT"
2920
next
@@ -34,20 +25,18 @@ def self.run(app, receive = "tcp://127.0.0.1:9997", send = "tcp://127.0.0.1:9996
3425
req.headers["PATTERN"].split('(', 2).first.gsub(/\/$/, '')
3526

3627
env = {
37-
"rack.version" => Rack::VERSION,
38-
"rack.url_scheme" => "http",
39-
"rack.input" => StringIO.new(req.body),
40-
"rack.errors" => $stderr,
41-
"rack.multithread" => true,
28+
"rack.version" => Rack::VERSION,
29+
"rack.url_scheme" => "http",
30+
"rack.input" => StringIO.new(req.body),
31+
"rack.errors" => $stderr,
32+
"rack.multithread" => true,
4233
"rack.multiprocess" => true,
43-
"rack.run_once" => false,
44-
45-
"mongrel2.pattern" => req.headers["PATTERN"],
46-
47-
"REQUEST_METHOD" => req.headers["METHOD"],
48-
"SCRIPT_NAME" => script_name,
49-
"PATH_INFO" => req.headers["PATH"].gsub(script_name, ''),
50-
"QUERY_STRING" => req.headers["QUERY"]
34+
"rack.run_once" => false,
35+
"mongrel2.pattern" => req.headers["PATTERN"],
36+
"REQUEST_METHOD" => req.headers["METHOD"],
37+
"SCRIPT_NAME" => script_name,
38+
"PATH_INFO" => req.headers["PATH"].gsub(script_name, ''),
39+
"QUERY_STRING" => req.headers["QUERY"]
5140
}
5241

5342
env["SERVER_NAME"], env["SERVER_PORT"] = req.headers["host"].split(':', 2)
@@ -61,7 +50,7 @@ def self.run(app, receive = "tcp://127.0.0.1:9997", send = "tcp://127.0.0.1:9996
6150
status, headers, rack_response = app.call(env)
6251
body = ""
6352
rack_response.each{|b| body << b}
64-
conn.reply_http(req, body, status, headers)
53+
connection.reply_http(req, body, status, headers)
6554
end
6655
end
6756
end

m2r.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Gem::Specification.new do |gem|
2222
gem.add_dependency "ffi", ">= 1.0.0"
2323
gem.add_dependency "json"
2424

25+
gem.add_development_dependency "rack"
2526
gem.add_development_dependency "rake"
2627
gem.add_development_dependency "minitest"
2728
end

0 commit comments

Comments
 (0)