Skip to content

Instantly share code, notes, and snippets.

@hkdnet
Last active October 28, 2019 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkdnet/9adfaa2234729ad51913b3169064dbc0 to your computer and use it in GitHub Desktop.
Save hkdnet/9adfaa2234729ad51913b3169064dbc0 to your computer and use it in GitHub Desktop.
require 'rack'
require 'rack/lobster'
class Streaming
class Stream
def each
%w[a b c].each do |e|
puts "Stream#each with #{e}"
sleep 1
yield e
end
end
end
def initialize(app)
@app = app
end
def call(env)
puts "call is called"
@app.call(env)
[
200,
{},
Stream.new
]
end
end
class Wrapper
def initialize(app)
@app = app
end
def call(env)
puts "wrapper start"
*res = @app.call(env)
puts "wrapper end"
res
end
end
use Wrapper
use Streaming
run Rack::Lobster.new
$ curl -i 'http://localhost:9292'
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Server: WEBrick/1.4.2 (Ruby/2.6.3/2019-04-16)
Date: Mon, 28 Oct 2019 03:54:59 GMT
Connection: Keep-Alive
abc%
$ curl --raw 'http://localhost:9292'
1
a
1
b
1
c
0
$ bundle exec rackup
[2019-10-28 15:23:25] INFO WEBrick 1.4.2
[2019-10-28 15:23:25] INFO ruby 2.6.3 (2019-04-16) [x86_64-darwin18]
[2019-10-28 15:23:25] INFO WEBrick::HTTPServer#start: pid=11206 port=9292
wrapper start
call is called
wrapper end
Stream#each with a
Stream#each with b
Stream#each with c
::1 - - [28/Oct/2019:15:23:36 +0900] "GET / HTTP/1.1" 200 - 3.0114
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment