Skip to content

Instantly share code, notes, and snippets.

@mostlyobvious
Created May 25, 2011 22:03
Show Gist options
  • Save mostlyobvious/992099 to your computer and use it in GitHub Desktop.
Save mostlyobvious/992099 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
$:<< '../lib' << 'lib'
require 'goliath'
# Our custom Goliath API
class HelloWorld < Goliath::API
def response(env)
[200, {}, "hello world!"]
end
end
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::Formatters::JSON # JSON output formatter
use Goliath::Rack::Render # 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: ' + data
(env['async-body'] ||= '') << data
end
def on_close(env)
env.logger.info 'closing connection'
end
def response(env)
[200, {}, {body: env['async-body'], head: env['async-headers']}]
end
end
class RackRoutes < Goliath::API
map "/hello_world" do
run HelloWorld.new
end
map '/' do
run AsyncUpload.new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment