-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.lua
217 lines (173 loc) · 6.14 KB
/
app.lua
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
--[[
Copyright (C) 2016 GoInto, LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
--]]
local lapis = require("lapis")
local app_helpers = require("lapis.application")
local validate = require("lapis.validate")
local config = require("lapis.config").get()
local rover = require("mongorover")
local client = rover.MongoClient.new("mongodb://localhost:27017/")
local database = client:getDatabase("gsm")
local server_collection = database:getCollection("servers")
local mp = require("MessagePack")
local ngx = require("ngx")
--local redis_client = redis.connect('127.0.0.1', 6379)
local app = lapis.Application()
local capture_errors = app_helpers.capture_errors
app.handle_error = function(self, err, trace)
ngx.log(ngx.NOTICE, "There was an error! " .. err .. ": " ..trace)
return { json = { error = err, trace = trace } }
end
app:enable("etlua")
app:get("/", function()
app.layout = require "views.layout"
if config.company then
page_title = (config.company .. " Server Tracker")
end
email = config.email
return {
render = "index"
}
end)
app:get("/robots.txt", function()
return { render = "robots" }
end)
app:get("/server", function(self)
local clock = os.time()
local timediff = clock - config.server_timeout
local showDevServers = false
if self.params.dev then
showDevServers = true
end
local result_servers = server_collection:find({ ping = { ["$gt"] = timediff }, dev = showDevServers })
local servers = {}
for server in result_servers do
if self.params.dev then
table.insert(servers, server)
else
table.insert(servers, {
hostname = server['hostname'],
port = server['port'],
name = server['name'],
activePlayers = server['activePlayers'],
maxPlayers = server['maxPlayers'],
ping = server['ping'],
})
end
end
return {
json = {
servers = servers
},
content_type = "application/json"
}
end)
app:post("/server", function()
return { redirect_to = "/" }
end)
-- TODO: app:delete("/server/ping")
app:post("/server/ping", capture_errors({
function(self)
local success
local reason
ngx.log(ngx.NOTICE, "PSK: " .. tostring(self.params.psk))
ngx.log(ngx.NOTICE, "hostname: " .. self.params.hostname)
ngx.log(ngx.NOTICE, "port: " .. self.params.port)
ngx.log(ngx.NOTICE, "name: " .. self.params.name)
ngx.log(ngx.NOTICE, "ping: " .. os.date("%Y-%m-%d %H:%M:%S"))
validate.assert_valid(self.params, {
{ "psk", exists = true, "Authentication failed" },
{ "name", exists = true, "Server name not proided." },
{ "hostname", exists = true, "Hostname not proided." },
{ "port", exists = true, "Port not proided." },
})
-- authenticate
if not self.params.psk or (self.params.psk ~= config.production_psk and self.params.psk ~= config.development_psk) then
ngx.log(ngx.WARN, "Authentication failure")
return {
json = {
success = false,
reason = "Authentication failure"
},
content_type = "application/json"
}
end
-- If activePlayers wasn't defined, let's assume there was none
local activePlayers
if self.params.activePlayers then
activePlayers = self.params.activePlayers
else
activePlayers = 0
end
-- If maxPlayers wasn't defined, let's default to 4
local maxPlayers
if self.params.maxPlayers then
maxPlayers = self.params.maxPlayers
else
maxPlayers = 4
end
-- Is this server a development server? We're going to decide by
-- using the PSK. There's one for prod and another for dev
local dev
if self.params.psk == config.production_psk then
dev = false
else
dev = true
end
-- define the document we're going to put in mongo
local server_doc = {
name = self.params.name,
ping = os.time(),
activePlayers = tonumber(activePlayers),
maxPlayers = tonumber(maxPlayers),
dev = dev,
}
-- update mongo
local result = server_collection:update_one(
{ hostname = self.params.hostname, port = self.params.port },
{["$set"] = server_doc},
true
)
if result.matched_count > 0 then
success = true
reason = "Successfully added server"
else
success = false
reason = "Could not add server"
end
ngx.log(ngx.NOTICE, reason)
return {
json = {
success = success,
reason = reason
},
content_type = "application/json"
}
end,
on_error = function(self, err, trace)
--ngx.log(ngx.NOTICE, "There was an error! " .. self.errors[0])
local error_string = ""
for i,v in pairs(self.errors) do
error_string = error_string .. " - " .. v
end
ngx.log(ngx.ERR, "ERROR" .. error_string)
return {
json = {
success = false,
reason = self.errors
},
content_type = "application/json"
}
end
}))
return app