Skip to content

Instantly share code, notes, and snippets.

@jpoz
Created February 12, 2012 23:20
Show Gist options
  • Save jpoz/1811712 to your computer and use it in GitHub Desktop.
Save jpoz/1811712 to your computer and use it in GitHub Desktop.
Goliath File Upload Proxy Server
require 'goliath'
require 'em-synchrony/em-http'
class UploadProxy < Goliath::API
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.size}"
(env['async-body'] ||= '') << data
end
def on_close(env)
env.logger.info 'closing connection'
end
def response(env)
gh = EM::HttpRequest.new("http://localhost:8080/upload")
.post(:head => env['async-headers'],
:body => env['async-body'])
gh.callback {
logger.info "Received #{gh.response_header.status} from Upstream"
env.stream_send gh.response
env.stream_close
}
gh.error {
env.stream_close
}
streaming_response(200, {'X-Stream' => 'Uploaddddd'})
end
end
require 'goliath'
require 'em-synchrony/em-http'
require './upload_proxy'
class HealthCheck < Goliath::API
def response(env)
[200, {}, ["OK"]]
end
end
class WithRoutes < Goliath::API
get "/healthcheck", HealthCheck
post "/upload", UploadProxy
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment