-
-
Save abdollar/1665360 to your computer and use it in GitHub Desktop.
chunked file stream 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 '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 |
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 '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 |
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 '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