-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ru
140 lines (117 loc) · 2.32 KB
/
config.ru
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
require 'securerandom'
require 'json'
require 'rack/cors'
require 'byebug'
require 'zeitwerk'
loader = Zeitwerk::Loader.new
loader.push_dir("#{__dir__}/lib")
loader.push_dir("#{__dir__}/web")
loader.setup
# TODO Development only setup
use Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post, :patch, :put]
end
end
use Rack::Static, {
urls: {
"/" => 'main.html',
"/src/out.js" => 'src/out.js'
},
root: 'clients'
}
GAMES = {}
begin
game_id = SecureRandom.hex(2)
end while GAMES[game_id]
GAMES[game_id] = Game.new(game_id)
class GameChoice
def initialize(app)
@app = app
end
def call(env)
if %w[/init /list / /games].include? env['REQUEST_PATH']
return @app.call(env)
end
game = GAMES[env["HTTP_MY_JOB_GAME_ID"]]
unless game
return [
404,
Constants::TEXT_TYPE,
[
{ error: "game not found. set HTTP_MY_JOB_GAME_ID to request header." +
"if not started yet, send init." }.to_json
]
]
end
env['GAME'] = game
@app.call(env)
end
end
app = Rack::Builder.new do
use Rack::ShowExceptions
use GameChoice
map "/init" do
run ->(env) do
begin
game_id = SecureRandom.hex(2)
end while GAMES[game_id]
GAMES[game_id] = Game.new(game_id)
[
200,
Constants::TEXT_TYPE,
[ "Game started with game_id: #{game_id}" ]
]
end
end
map "/destroy" do
# TODO double check?
run ->(env) do
game = env['GAME']
GAMES.delete(game.game_id)
[
200,
Constants::TEXT_TYPE,
[ "Game #{game.game_id} destroyed. It was nice running game with you." ]
]
end
end
map "/games" do
# TODO some authentication
run ->(env) do
[
200,
Constants::TEXT_TYPE,
[ GAMES.keys.join("\n") ]
]
end
end
map "/action" do
run OneOffActionHandler
end
map "/schedule" do
run ScheduleHandler
end
map "/list" do
run ListHandler
end
map "/logs" do
run LogHandler
end
map "/change_speed" do
run ChangeSpeedHandler
end
map "/stats" do
run ->(env) do
[
200,
Constants::JSON_TYPE,
[
Aggregates::Stats.new(env['GAME']).call.to_h.to_json
]
]
end
end
end
run app