Skip to content

Instantly share code, notes, and snippets.

@jeremyvdw
Forked from elucid/async_upload.rb
Created January 18, 2012 00:17
Show Gist options
  • Save jeremyvdw/1629962 to your computer and use it in GitHub Desktop.
Save jeremyvdw/1629962 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