Skip to content

Instantly share code, notes, and snippets.

@elucid
Created November 21, 2011 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elucid/1384021 to your computer and use it in GitHub Desktop.
Save elucid/1384021 to your computer and use it in GitHub Desktop.
Running multiple Goliath apps
require 'goliath'
require 'yajl'
# adapted from goliath/examples/async_upload.rb
class AsyncUpload < Goliath::API
use Goliath::Rack::Params # parse & merge query and body parameters
use Goliath::Rack::DefaultMimeType # cleanup accepted media types
use Goliath::Rack::Render, 'json' # auto-negotiate response format
def on_headers(env, headers)
env.logger.info 'received headers: ' + headers.inspect
env['async-headers'] = headers
end
def on_body(env, data)
env.logger.info "received #{data.size} bytes of data"
(env['async-body'] ||= '') << data
end
def on_close(env)
env.logger.info 'closing connection'
end
def response(env)
[200, {}, {body: env['async-body'].size.to_s, head: env['async-headers']}]
end
end
require 'goliath'
class OtherEndpoint < Goliath::API
def response(env)
[200, {}, "this is the other endpoint"]
end
end
$:.unshift '.'
require 'goliath'
require 'other_endpoint'
require 'async_upload'
class Router < Goliath::API
map '/other_endpoint' do
run OtherEndpoint.new
end
map '/async_upload' do
use Goliath::Rack::Validation::RequestMethod, %w(POST)
run AsyncUpload.new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment