-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
165 lines (130 loc) · 3.22 KB
/
app.rb
File metadata and controls
165 lines (130 loc) · 3.22 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require 'sinatra/base'
require 'sinatra-websocket'
require 'redis'
require 'redis-objects'
require 'connection_pool'
require 'json'
require 'logger'
require_relative './counter.rb'
class SaikohTk < Sinatra::Base
configure do
set :redis_url, ENV['REDIS_URL'] || 'redis://localhost:6379'
Redis::Objects.redis = ConnectionPool.new(size: 5, timeout: 5) { Redis.new(url: settings.redis_url) }
end
helpers do
def counters
Thread.current[:counters] ||= [
Counter.new(1, 'saikoh', '最高'),
Counter.new(2, 'emoi', 'エモい'),
Counter.new(3, 'imagine_the_future', 'IMAGINE THE FUTURE'),
Counter.new(4, 'we_are_the_champions', 'We Are the Champions')
]
end
def logger
Dir.mkdir('log') unless Dir.exist?('log')
Thread.current[:logger] ||= Logger.new('log/logfile.log')
end
def find_counter_by_name(name)
counter = counters.find do |counter|
counter.name.value == name
end
end
def meta_description
counters.map do |counter|
"#{counter.label.value}: #{counter.count.value}"
end.join(", ")
end
def incr_counter(counter)
count = counter.count.incr
data = {
type: 'update',
data: counter.as_json
}.to_json
websockets.each do |ws|
ws.send(data)
end
log(counter.name, {
ip: request.ip,
user_agent: request.user_agent
})
return count
end
def log(event_type, host)
timestamp = Time.now.to_f
logger.info(JSON.generate({
event: event_type,
host: {
ip: host[:ip],
user_agent: host[:user_agent]
}
}))
end
def websockets
Thread.current[:websockets] ||= []
end
def handle_message(message)
case message[:type]
when "incr"
name = message.dig(:data, :name)
counter = find_counter_by_name(name)
incr_counter(counter) unless counter.nil?
end
end
end
#
# Web endpoints
#
get '/' do
@counters = counters
slim :index
end
get '/:counter/incr' do
counter = find_counter_by_name(params[:counter])
halt 400 if counter.nil?
incr_counter(counter)
@counter = counter
slim :play
end
#
# JSON APIs
#
get '/api/counters' do
content_type :json
counters.map(&:to_json)
end
get '/api/:counter/show' do
counter = find_counter_by_name(params[:counter])
halt 400 if counter.nil?
content_type :json
counter.to_json
end
get '/api/:counter/incr' do
counter = find_counter_by_name(params[:counter])
halt 400 if counter.nil?
incr_counter(counter)
content_type :json
counter.to_json
end
#
# WebSocket APIs
# Hidden feature: A trailing '.' will appear after 'ITF.' when a WebSocket connection is established!
#
get '/api/websocket' do
halt 404 unless request.websocket?
request.websocket do |ws|
ws.onopen do
websockets.push(ws)
end
ws.onclose do
websockets.delete(ws)
end
ws.onmessage do |msg|
begin
message = JSON.parse(msg, symbolize_names: true)
rescue JSON::ParserError => e
end
handle_message(message)
end
end
end
end