forked from perplexes/m2r
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It works! Add a first-pass rackup adapter which also works.
- Loading branch information
Showing
4 changed files
with
139 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,24 @@ | ||
This is almost verbatim from Zed Shaw's python connector for mongrel2, which you can see here: http://mongrel2.org/dir?ci=1bdfff8f050b97df&name=examples/python/mongrel2 | ||
m2r | ||
=== | ||
|
||
You'll need a recent 1.9 ruby and FFI and http://github.com/chuckremes/ffi-rzmq, oh, and Zero MQ. Run: `ruby example/http_0mq.rb` | ||
A [mongrel2](http://mongrel2.org/index) backend handler written in Ruby, based on [Zed's Python backend handler](http://mongrel2.org/dir?ci=1bdfff8f050b97df&name=examples/python/mongrel2). | ||
|
||
Usage/Examples | ||
----- | ||
|
||
* `examples/http_0mq.rb` is a test little servlet thing (based on what comes with mongrel2)and there is also a rack handler at | ||
* `examples/rack_handler.rb` is a Mongrel2 ruby handler rack handler mouthful, whose variables are probably a little off | ||
* `examples/lobster.ru` is a rackup file using the Rack handler that'll serve Rack's funny little lobster app. | ||
|
||
Run: | ||
* `ruby examples/http_0mq.rb`, which with Mongrel2's test config will serve up at http://localhost:6767/handlertest | ||
* `rackup examples/lobster.ru`, ditto, http://localhost:6767/handlertest | ||
|
||
Installation | ||
------------ | ||
|
||
* Ruby 1.9ish (RVM saved my life here) | ||
* [FFI](http://github.com/ffi/ffi), `gem install ffi` should be fine | ||
* [Zero MQ](http://www.zeromq.org/area:download) | ||
* [ffi-rzmq](http://github.com/chuckremes/ffi-rzmq), which you'll have to build. The native zmq didn't work for me, but if you want to fix it, please do! | ||
* Rack (gem install rack) if you want to run the rack example. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'rack/lobster' | ||
$: << File.dirname(__FILE__) | ||
require 'rack_handler' | ||
|
||
use Rack::ShowExceptions | ||
puts "Lobster at http://localhost:6767/handlertest" | ||
Rack::Handler::Mongrel2Handler.run Rack::Lobster.new |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'rubygems' | ||
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__) + '/..') | ||
require 'connection' | ||
|
||
$sender_id = "70D107AB-19F5-44AE-A2D0-2326A167D8D7" | ||
|
||
module Rack | ||
module Handler | ||
class Mongrel2Handler | ||
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) | ||
while true | ||
puts "WAITING FOR REQUEST" | ||
|
||
req = conn.recv | ||
|
||
if req.is_disconnect | ||
puts "DICONNECT" | ||
next | ||
end | ||
|
||
env = { | ||
"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, | ||
"REQUEST_METHOD" => req.headers["METHOD"], | ||
"SCRIPT_NAME" => "", | ||
"PATH_INFO" => env["PATH"], | ||
"QUERY_STRING" => env["QUERY"] | ||
} | ||
|
||
env["SERVER_NAME"], env["SERVER_PORT"] = req.headers["Host"].split(':', 2) | ||
req.headers.each do |key, val| | ||
unless key =~ /content_(type|length)/i | ||
key = "HTTP_#{key.upcase}" | ||
end | ||
env[key] = val | ||
end | ||
|
||
status, headers, rack_response = app.call(env) | ||
conn.reply_http(req, rack_response.body.join, status, headers) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|