-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp-server.go
300 lines (266 loc) · 6.77 KB
/
http-server.go
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"goFactServView/cwlog"
"math"
"net/http"
"strconv"
"strings"
)
const (
SORT_PLAYER = iota
SORT_NAME
SORT_DESC
SORT_TIME
SORT_RTIME
)
// HTTP request handler
func reqHandle(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
return
}
//If this isn't a query, pass to file server
if !strings.EqualFold(r.RequestURI, "/") && !strings.HasPrefix(r.RequestURI, "/?") {
fileServer.ServeHTTP(w, r)
return
}
FetchLock.Lock()
//If needed, refresh data
fetchServerList()
//Build temporary server params
var tempParams *ServerStateData = &ServerStateData{
URL: sParam.URL,
Query: sParam.Query,
Token: sParam.Token,
Username: sParam.Username,
LastRefresh: sParam.LastRefresh,
LastAttempt: sParam.LastAttempt,
UserAgent: sParam.UserAgent,
ServerList: sParam.ServerList,
ItemsPerPage: ItemsPerPage,
VersionList: sParam.VersionList,
PlayerCount: sParam.PlayerCount,
}
FetchLock.Unlock()
//Create a blank server list
page := 1
//Log request
cwlog.DoLog(false, "Request: %v", r.RequestURI)
sortBy := SORT_PLAYER
filterFound := false
queryItems := r.URL.Query()
if len(queryItems) > 0 {
for key, values := range queryItems {
//Skip if invalid
if len(key) == 0 || len(values) == 0 {
continue
}
if strings.EqualFold(key, "version") {
tempParams.FVersion = values[0]
}
if strings.EqualFold(key, "vanilla") {
tempParams.VanillaOnly = true
tempParams.ModdedOnly = false
} else if strings.EqualFold(key, "modded") {
tempParams.VanillaOnly = false
tempParams.ModdedOnly = true
} else if strings.EqualFold(key, "both") {
tempParams.VanillaOnly = false
tempParams.ModdedOnly = false
}
if strings.EqualFold(key, "haspass") {
tempParams.HasPass = true
} else if strings.EqualFold(key, "anypass") {
tempParams.AnyPass = true
}
if strings.EqualFold(key, "hasplay") {
tempParams.HasPlay = true
} else if strings.EqualFold(key, "noplay") {
tempParams.NoPlay = true
}
//Don't parse multiple searches
if !filterFound {
if strings.EqualFold(key, "name") {
filterFound = true
if values[0] == "" {
continue
}
tempParams.Searched = values[0]
tempParams.FName = true
} else if strings.EqualFold(key, "desc") {
filterFound = true
if values[0] == "" {
continue
}
tempParams.Searched = values[0]
tempParams.FDesc = true
} else if strings.EqualFold(key, "tag") {
filterFound = true
if values[0] == "" {
continue
}
tempParams.Searched = values[0]
tempParams.FTag = true
} else if strings.EqualFold(key, "player") {
filterFound = true
if values[0] == "" {
continue
}
tempParams.Searched = values[0]
tempParams.FPlayer = true
}
}
//Parse sorting arguments
if strings.EqualFold(key, "sort-players") {
sortBy = SORT_PLAYER
tempParams.SPlayers = true
} else if strings.EqualFold(key, "sort-name") {
sortBy = SORT_NAME
tempParams.SName = true
} else if strings.EqualFold(key, "sort-time") {
sortBy = SORT_TIME
tempParams.STime = true
} else if strings.EqualFold(key, "sort-rtime") {
sortBy = SORT_RTIME
tempParams.SRTime = true
} else if strings.EqualFold(key, "page") {
val, err := strconv.ParseUint(values[0], 10, 64)
if err != nil {
continue
} else {
page = int(val)
}
}
}
}
//Filter, sort, paginate
filterServers(tempParams)
tempParams.ServerList.Servers = sortServers(!filterFound, tempParams.ServerList.Servers, sortBy)
paginateList(page, tempParams)
//Execute template
err := tmpl.Execute(w, tempParams)
if err != nil {
cwlog.DoLog(true, "Error: %v", err)
}
}
func filterServers(tempParams *ServerStateData) {
var tempServers []ServerListItem
lSearch := strings.ToLower(tempParams.Searched)
for _, server := range tempParams.ServerList.Servers {
//Order: Fastest compairsons that remove the most items first.
//Password
if !tempParams.AnyPass {
if tempParams.HasPass && !server.Has_password {
continue
}
if !tempParams.HasPass && server.Has_password {
continue
}
}
//Players
if tempParams.HasPlay && !server.Local.HasPlayers {
continue
}
if tempParams.NoPlay && server.Local.HasPlayers {
continue
}
//Modded
if tempParams.ModdedOnly && !server.Local.Modded {
continue
}
if tempParams.VanillaOnly && server.Local.Modded {
continue
}
//Version
if tempParams.FVersion != "" &&
server.Application_version.Game_version != tempParams.FVersion {
continue
}
//Search Type
if lSearch != "" {
if tempParams.FName {
if server.Name == "" {
continue
}
if !strings.Contains(strings.ToLower(server.Name), lSearch) {
continue
}
} else if tempParams.FDesc {
if server.Description == "" {
continue
}
if !strings.Contains(strings.ToLower(server.Description), lSearch) {
continue
}
} else if tempParams.FTag {
found := false
for _, tag := range server.Tags {
if tag == "" {
continue
}
if strings.Contains(strings.ToLower(tag), lSearch) {
found = true
break
}
}
if !found {
continue
}
} else if tempParams.FPlayer {
found := false
for _, player := range server.Players {
if player == "" {
continue
}
if strings.Contains(strings.ToLower(player), lSearch) {
found = true
break
}
}
if !found {
continue
}
}
}
tempServers = append(tempServers, server)
}
tempParams.ServerList.Servers = tempServers
tempParams.ServersCount = len(tempParams.ServerList.Servers)
}
// Present a single page of results
func paginateList(page int, tempParams *ServerStateData) {
if page < 1 {
page = 1
}
//Calculate list position
pageStart := (page - 1) * ItemsPerPage
pageEnd := page * ItemsPerPage
//Build temp list
tempServerList := []ServerListItem{}
//Calculate start/end of page
if pageEnd > tempParams.ServersCount {
pageEnd = tempParams.ServersCount
}
if pageStart > tempParams.ServersCount {
pageStart = tempParams.ServersCount - tempParams.ItemsPerPage
}
//Reject invalid page
if pageStart < 0 {
cwlog.DoLog(true, "Page start less than 0.")
return
}
//Put results into temp list
for c := pageStart; c < pageEnd; c++ {
tempServerList = append(tempServerList, tempParams.ServerList.Servers[c])
}
//Put results into page
tempParams.ServerList.Servers = tempServerList
tempParams.NumPages = int(math.Ceil(float64(tempParams.ServersCount) / float64(tempParams.ItemsPerPage)))
//Handle invalid or nil page numbers
if page > tempParams.NumPages {
tempParams.CurrentPage = tempParams.NumPages
} else if page < 1 {
tempParams.CurrentPage = 0
} else {
tempParams.CurrentPage = page
}
}