Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@grantr
Created August 23, 2012 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grantr/3442964 to your computer and use it in GitHub Desktop.
Save grantr/3442964 to your computer and use it in GitHub Desktop.
Reel-powered http endpoints
require 'octarine'
require 'reel'
module Reel
module App
def self.included(base)
base.class_eval do
include Octarine::App
attr_accessor :server
end
end
def initialize(host, port)
super()
@server = Reel::Server.supervise host, port do |connection|
if connection.request # why is this nil sometimes?
request = connection.request
status, headers, body = call Rack::MockRequest.env_for(request.url, method: request.method, input: request.body)
connection.respond status_symbol(status), headers, body.to_s
end
end
end
def status_symbol(status)
status.is_a?(Fixnum) ? Http::Response::STATUS_CODES[status].downcase.gsub(/\s|-/, '_').to_sym : status.to_sym
end
def terminate
@server.terminate
end
end
end
class MyApp
include Reel::App
get "/foo" do |request|
[200, {}, "hello foo"]
end
end
MyApp.new("0.0.0.0", 3010)
# curl 127.0.0.1:3010/foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment