-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
executable file
·42 lines (35 loc) · 1 KB
/
server.rb
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
40
41
42
#!/usr/bin/env ruby
$:.unshift File.expand_path(File.dirname(__FILE__) + '/lib')
require 'goliath'
require 'json'
require 'haml'
require 'wormbots'
class Server < Goliath::API
# render templated files from ./views
include Goliath::Rack::Templates
# render static files from ./public
use Rack::Static,
:root => Goliath::Application.app_path('public'),
:urls => ['/stylesheets', '/javascripts', '/images']
def on_close(env)
if env['subscription']
env.logger.info('Connection closed')
env.channel.unsubscribe(env['subscription'])
end
end
def on_error(env, error)
env.logger.error(error)
end
def response(env)
case env['REQUEST_PATH']
when '/'
@start_time = Time.at(config['start_time']).iso8601
[200, {}, haml(:index)]
when '/world'
env['subscription'] = env.channel.subscribe do |message|
env.stream_send("data: #{message.to_json}\n\n")
end
streaming_response(200, {'Content-Type' => 'text/event-stream'})
end
end
end