Created
May 25, 2011 20:32
-
-
Save mostlyobvious/991872 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 "/upload" do | |
run AsyncUpload.new | |
end | |
map '/' do | |
run Proc.new { |env| [404, {"Content-Type" => "text/html"}, ["try: /upload or /hello_world"]] } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment