forked from postrank-labs/goliath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_stream.rb
More file actions
executable file
·39 lines (30 loc) · 996 Bytes
/
Copy pathcontent_stream.rb
File metadata and controls
executable file
·39 lines (30 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env ruby
$:<< '../lib' << 'lib'
require 'goliath'
# This example assumes you have an AMQP server up and running with the
# following config (using rabbit-mq as an example)
#
# rabbitmq-server start
# rabbitmqctl add_vhost /test
# rabbitmqctl add_user test test
# rabbitmqctl set_permissions -p /test test ".*" ".*" ".*"
class ContentStream < Goliath::API
use Goliath::Rack::Params
use Goliath::Rack::Render, 'json'
use Goliath::Rack::Heartbeat
use Goliath::Rack::Validation::RequestMethod, %w(GET)
def on_close(env)
# This is just to make sure if the Heartbeat fires we don't try
# to close a connection.
return unless env['subscription']
env.channel.unsubscribe(env['subscription'])
env.logger.info "Stream connection closed."
end
def response(env)
env.logger.info "Stream connection opened"
env['subscription'] = env.channel.subscribe do |msg|
env.stream_send(msg)
end
[200, {}, Goliath::Response::STREAMING]
end
end