Skip to content

Instantly share code, notes, and snippets.

@igrigorik
Created January 26, 2012 07:12
Show Gist options
  • Save igrigorik/1681479 to your computer and use it in GitHub Desktop.
Save igrigorik/1681479 to your computer and use it in GitHub Desktop.
stream from http source to client with Goliath
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