Skip to content

Instantly share code, notes, and snippets.

@igrigorik
Created January 22, 2012 21:12
Show Gist options
  • Save igrigorik/1658848 to your computer and use it in GitHub Desktop.
Save igrigorik/1658848 to your computer and use it in GitHub Desktop.
chunked file stream with goliath
require 'bundler'
Bundler.require
url = 'http://0.0.0.0:9000/images/avatar.png'
EM.run do
http = EventMachine::HttpRequest.new(url).get
http.stream {|chunk| print [:chunk, chunk.size] }
http.headers {|h| p [:headers, h] }
http.callback do
p ["Done", http.response.size]
EM.stop
end
http.errback do
p ["Error", http]
EM.stop
end
end
require 'uri'
module Store
class FileSystem
CHUNKSIZE = 65536
def initialize(uri)
@uri = uri
end
def get
open(@uri, "rb") do |file|
yield file.read(CHUNKSIZE) until file.eof?
end
end
end
end
require 'bundler'
Bundler.require
require 'file_system'
class GetImage < Goliath::API
use Goliath::Rack::Render, 'json'
def response(env)
uri = params[:id]
headers = {'X-filename' => params[:id]}
operation = proc do
Store::FileSystem.new(uri).get { |chunk| env.chunked_stream_send(chunk) }
end
callback = proc do |result|
env.chunked_stream_close
end
EM.defer operation, callback
headers.merge!( 'X-Stream' => 'Goliath')
chunked_streaming_response(200, headers)
end
def on_close(env)
env.logger.info "Stream closed"
end
end
class Server < Goliath::API
use Goliath::Rack::Heartbeat
use Goliath::Rack::Params
get '/images/:id', GetImage
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment