Class: Webmachine::Adapters::Rack
- Inherits:
-
Webmachine::Adapter
- Object
- Webmachine::Adapter
- Webmachine::Adapters::Rack
- Defined in:
- lib/webmachine/adapters/rack.rb
Overview
A minimal “shim” adapter to allow Webmachine to interface with Rack. The intention here is to allow Webmachine to run under Rack-compatible web-servers, like unicorn and pow. The adapter expects your Webmachine application to be mounted at the root path - it will NOT allow you to nest your Webmachine application at an arbitrary path eg. map “/api” { run MyWebmachineAPI }
To use this adapter, create a config.ru file and populate it like so:
require 'webmachine/adapters/rack'
# put your own Webmachine resources in another file:
require 'my/resources'
run MyApplication.adapter
Servers like pow and unicorn will read config.ru by default and it should all “just work”.
And for development or testing your application can be run with Rack’s builtin Server identically to the WEBrick adapter with:
MyApplication.run
Defined Under Namespace
Classes: RackResponse, RequestBody
Constant Summary collapse
- DEFAULT_OPTIONS =
Used to override default Rack server options (useful in testing)
{}
- REQUEST_URI =
'REQUEST_URI'.freeze
- VERSION_STRING =
"#{Webmachine::SERVER_STRING} Rack/#{::Rack.version}".freeze
- NEWLINE =
"\n".freeze
Instance Attribute Summary
Attributes inherited from Webmachine::Adapter
Instance Method Summary collapse
-
#call(env) ⇒ Object
Handles a Rack-based request.
-
#run ⇒ Object
Start the Rack adapter.
Methods inherited from Webmachine::Adapter
Constructor Details
This class inherits a constructor from Webmachine::Adapter
Instance Method Details
#call(env) ⇒ Object
Handles a Rack-based request.
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 |
# File 'lib/webmachine/adapters/rack.rb', line 58 def call(env) headers = Webmachine::Headers.from_cgi(env) rack_req = ::Rack::Request.new env request = Webmachine::Request.new(rack_req.request_method, rack_req.url, headers, RequestBody.new(rack_req)) response = Webmachine::Response.new application.dispatcher.dispatch(request, response) response.headers[SERVER] = VERSION_STRING rack_status = response.code rack_headers = response.headers.flattened(NEWLINE) rack_body = case response.body when String # Strings are enumerable in ruby 1.8 [response.body] else if (io_body = IO.try_convert(response.body)) io_body elsif response.body.respond_to?(:call) Webmachine::ChunkedBody.new(Array(response.body.call)) elsif response.body.respond_to?(:each) # This might be an IOEncoder with a Content-Length, which shouldn't be chunked. if response.headers[TRANSFER_ENCODING] == "chunked" Webmachine::ChunkedBody.new(response.body) else response.body end else [response.body.to_s] end end rack_res = RackResponse.new(rack_body, rack_status, rack_headers) rack_res.finish end |
#run ⇒ Object
Start the Rack adapter
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/webmachine/adapters/rack.rb', line 45 def run = DEFAULT_OPTIONS.merge({ :app => self, :Port => application.configuration.port, :Host => application.configuration.ip }).merge(application.configuration.) @server = ::Rack::Server.new() @server.start end |