Skip to content

Commit

Permalink
Revise examples and provide starting guide.
Browse files Browse the repository at this point in the history
  • Loading branch information
mostlyobvious committed Jul 13, 2012
1 parent d7b41a9 commit adbc14e
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 52 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ spec/reports
test/tmp
test/version_tmp
tmp
example/tmp
example/config.sqlite
49 changes: 30 additions & 19 deletions example/http_0mq.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# http_0mq.rb - an example handler from the Mongrel2 book
# You can spin up many of these - Mongrel2 will then round-robin requests to each one.
# 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:
#
# bundle exec ruby -I../lib http_0mq.rb
#
# m2sh load -config mongrel2.conf
# m2sh start -name main
#
# curl http://localhost:6767

# require 'rubygems'
# require 'ruby-debug'
# Debugger.start
$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'm2r'

class Http0MQHandler < Mongrel2::Handler
# There are more hooks you can override - check out lib/handler.rb
require 'm2r/handler'
require 'securerandom'

class Http0MQHandler < M2R::Handler
def on_wait
puts "WAITING FOR REQUEST"
end
Expand All @@ -18,16 +22,23 @@ def on_disconnect
puts "DISCONNECT"
end

def process(req)
response = "<pre>\nSENDER: %s\nIDENT:%s\nPATH: %s\nHEADERS:%s\nBODY:%s</pre>" % [
req.sender.inspect, req.conn_id.inspect, req.path.inspect,
JSON.generate(req.headers).inspect, req.body.inspect]
puts response
response
def process(request)
<<EOF
<pre>
SENDER: #{request.sender}
IDENT: #{request.conn_id}
PATH: #{request.path}
HEADERS: #{JSON.pretty_generate(request.headers)}
BODY: #{request.body.inspect}
</pre>
EOF
end
end

sender_id = "C2256F34-14A1-45DD-BB73-97CAE25E25B4"
handler = Http0MQHandler.new(
sender_id, "tcp://127.0.0.1:9997", "tcp://127.0.0.1:9996")
sender_id = SecureRandom.uuid
pull_port = "tcp://127.0.0.1:9997"
pub_port = "tcp://127.0.0.1:9996"

handler = Http0MQHandler.new(sender_id, pull_port, pub_port)
handler.listen

18 changes: 14 additions & 4 deletions example/lobster.ru
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
# An example of running Rack application.
#
# Running this example:
#
# bundle exec rackup -I../lib lobster.ru
#
# m2sh load -config mongrel2.conf
# m2sh start -name main
#
# curl http://localhost:6767

require 'rack/lobster'
$: << ::File.dirname(__FILE__)
require 'rack_handler'
require './rack_handler'

use Rack::ShowExceptions
puts "Lobster at http://localhost:6767/handlertest"
Rack::Handler::Mongrel2.run Rack::Lobster.new
Rack::Handler::Mongrel2.run Rack::Lobster.new

26 changes: 26 additions & 0 deletions example/mongrel2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
handler_test = Handler(
send_spec = "tcp://127.0.0.1:9997",
send_ident = "34f9ceee-cd52-4b7f-b197-88bf2f0ec378",
recv_spec = "tcp://127.0.0.1:9996",
recv_ident = ""
)

mongrel2 = Host(
name = "127.0.0.1",
routes = { "/": handler_test }
)

main = Server(
uuid="2f62bd5-9e59-49cd-993c-3b6013c28f05",
access_log = "/tmp/access.log",
error_log = "/tmp/error.log",
chroot = "./",
pid_file = "/tmp/mongrel2.pid",
default_host = "127.0.0.1",
name = "main",
port = 6767,
hosts = [mongrel2]
)

servers = [main]

47 changes: 18 additions & 29 deletions example/rack_handler.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
require 'rubygems'
require 'm2r/connection'
require 'rack'
require 'stringio'
# require 'ruby-debug'
# Debugger.start
# gem install ruby-debug19 -- --with-ruby-include=$HOME/.rvm/src/ruby-1.9.2-head

$: << ::File.expand_path(::File.dirname(__FILE__) + '/../lib')
require 'connection'

$sender_id = "70D107AB-19F5-44AE-A2D0-2326A167D8D7"
require 'securerandom'

module Rack
module Handler
class Mongrel2
def self.run(app, receive = "tcp://127.0.0.1:9997", send = "tcp://127.0.0.1:9996")
conn = ::Mongrel2::Connection.new($sender_id, receive, send)
@running = true
trap("SIGINT") do
@running = false
end
connection = M2R::Connection.new(SecureRandom.uuid, receive, send)
@running = true
trap("SIGINT") { @running = false }

while @running
puts "WAITING FOR REQUEST"

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

env = {
"rack.version" => Rack::VERSION,
"rack.url_scheme" => "http",
"rack.input" => StringIO.new(req.body),
"rack.errors" => $stderr,
"rack.multithread" => true,
"rack.version" => Rack::VERSION,
"rack.url_scheme" => "http",
"rack.input" => StringIO.new(req.body),
"rack.errors" => $stderr,
"rack.multithread" => true,
"rack.multiprocess" => true,
"rack.run_once" => false,

"mongrel2.pattern" => req.headers["PATTERN"],

"REQUEST_METHOD" => req.headers["METHOD"],
"SCRIPT_NAME" => script_name,
"PATH_INFO" => req.headers["PATH"].gsub(script_name, ''),
"QUERY_STRING" => req.headers["QUERY"]
"rack.run_once" => false,
"mongrel2.pattern" => req.headers["PATTERN"],
"REQUEST_METHOD" => req.headers["METHOD"],
"SCRIPT_NAME" => script_name,
"PATH_INFO" => req.headers["PATH"].gsub(script_name, ''),
"QUERY_STRING" => req.headers["QUERY"]
}

env["SERVER_NAME"], env["SERVER_PORT"] = req.headers["host"].split(':', 2)
Expand All @@ -61,7 +50,7 @@ def self.run(app, receive = "tcp://127.0.0.1:9997", send = "tcp://127.0.0.1:9996
status, headers, rack_response = app.call(env)
body = ""
rack_response.each{|b| body << b}
conn.reply_http(req, body, status, headers)
connection.reply_http(req, body, status, headers)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions m2r.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Gem::Specification.new do |gem|
gem.add_dependency "ffi", ">= 1.0.0"
gem.add_dependency "json"

gem.add_development_dependency "rack"
gem.add_development_dependency "rake"
gem.add_development_dependency "minitest"
end

0 comments on commit adbc14e

Please sign in to comment.