Created
January 26, 2012 07:12
-
-
Save igrigorik/1681479 to your computer and use it in GitHub Desktop.
stream from http source to client with Goliath
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
require 'goliath' | |
require 'json' | |
require "em-synchrony" | |
require "em-synchrony/em-http" | |
class GetImage < Goliath::API | |
def response(env) | |
@uri = ('http://dl.dropbox.com/u/3528102/10.iso') #10mb file | |
EM.next_tick do | |
http = EventMachine::HttpRequest.new(@uri).aget | |
finish = proc { env.chunked_stream_close } | |
http.stream { |chunk| env.chunked_stream_send chunk } | |
http.callback &finish | |
http.errback &finish | |
end | |
headers = {'Content-Type' => 'application/octet-stream', | |
'X-Stream' => 'Goliath'} | |
chunked_streaming_response(200, headers) | |
end | |
def on_close(env) | |
env.logger.info "Connection closed" | |
end | |
end | |
class Server < Goliath::API | |
# use Goliath::Rack::Params | |
get '/image', GetImage | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment