-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers_admin.go
More file actions
420 lines (367 loc) · 10 KB
/
handlers_admin.go
File metadata and controls
420 lines (367 loc) · 10 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// ABOUTME: Admin dashboard handlers for SleeperPy
// ABOUTME: Provides real-time metrics, usage statistics, and operational visibility
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net"
"net/http"
"os"
"runtime"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
)
// Admin metrics tracking (in-memory)
var adminMetrics = struct {
sync.RWMutex
startTime time.Time
pageViews map[string]int64 // path -> count
userAgents map[string]int64 // user agent -> count
activeUsers int64 // users in last 24h
dynastyPercent float64 // % of leagues that are dynasty
errorLog []ErrorLog
}{
startTime: time.Now(),
pageViews: make(map[string]int64),
userAgents: make(map[string]int64),
errorLog: make([]ErrorLog, 0, 100),
}
type ErrorLog struct {
Timestamp time.Time
Path string
Error string
UserAgent string
}
type AdminData struct {
// Server Info
Uptime string
GoVersion string
Goroutines int
MemoryUsage string
ServerTime string
// Metrics from Prometheus
TotalVisitors float64
TotalLookups float64
TotalLeagues float64
TotalTeams float64
TotalErrors float64
// Rate metrics (calculated)
LookupsPerHour float64
LeaguesPerHour float64
// Additional metrics
PageViews map[string]int64
TopUserAgents []UACount
RecentErrors []ErrorLog
DynastyPercent float64
}
type UACount struct {
UserAgent string
Count int64
}
type PublicStatusData struct {
Uptime string
UpdatedAt string
ServiceStatus string
ErrorRate string
TotalLookups float64
TotalLeagues float64
TotalErrors float64
LookupsPerHour float64
LeaguesPerHour float64
}
func publicStatusHandler(w http.ResponseWriter, r *http.Request) {
lookups := getMetricValue(totalLookups)
leagues := getMetricValue(totalLeagues)
errors := getMetricValue(totalErrors)
uptime := time.Since(adminMetrics.startTime)
uptimeHours := uptime.Hours()
lookupsPerHour := 0.0
leaguesPerHour := 0.0
if uptimeHours > 0 {
lookupsPerHour = lookups / uptimeHours
leaguesPerHour = leagues / uptimeHours
}
denom := lookups
if denom < 1 {
denom = 1
}
errorRate := (errors / denom) * 100.0
serviceStatus := "Operational"
if lookups == 0 {
serviceStatus = "Initializing"
} else if errorRate >= 5.0 {
serviceStatus = "Degraded"
}
data := PublicStatusData{
Uptime: uptime.Round(time.Second).String(),
UpdatedAt: time.Now().Format("2006-01-02 15:04:05 MST"),
ServiceStatus: serviceStatus,
ErrorRate: fmt.Sprintf("%.1f%%", errorRate),
TotalLookups: lookups,
TotalLeagues: leagues,
TotalErrors: errors,
LookupsPerHour: lookupsPerHour,
LeaguesPerHour: leaguesPerHour,
}
tmpl := template.Must(template.ParseFiles("templates/status.html"))
if err := tmpl.Execute(w, data); err != nil {
http.Error(w, "Error rendering status page", http.StatusInternalServerError)
log.Printf("Error rendering status.html: %v", err)
}
}
func adminHandler(w http.ResponseWriter, r *http.Request) {
if !adminAccessAllowed(r) {
w.WriteHeader(http.StatusUnauthorized)
tmpl := template.Must(template.ParseFiles("templates/admin_unauthorized.html"))
_ = tmpl.Execute(w, map[string]string{
"ServerTime": time.Now().Format("2006-01-02 15:04:05 MST"),
})
return
}
data := AdminData{
Uptime: time.Since(adminMetrics.startTime).Round(time.Second).String(),
GoVersion: runtime.Version(),
Goroutines: runtime.NumGoroutine(),
MemoryUsage: getMemoryUsage(),
ServerTime: time.Now().Format("2006-01-02 15:04:05 MST"),
TotalVisitors: getMetricValue(totalVisitors),
TotalLookups: getMetricValue(totalLookups),
TotalLeagues: getMetricValue(totalLeagues),
TotalTeams: getMetricValue(totalTeams),
TotalErrors: getMetricValue(totalErrors),
}
// Calculate rate metrics
uptimeHours := time.Since(adminMetrics.startTime).Hours()
if uptimeHours > 0 {
data.LookupsPerHour = data.TotalLookups / uptimeHours
data.LeaguesPerHour = data.TotalLeagues / uptimeHours
}
// Add in-memory metrics
adminMetrics.RLock()
data.PageViews = make(map[string]int64)
for k, v := range adminMetrics.pageViews {
data.PageViews[k] = v
}
data.DynastyPercent = adminMetrics.dynastyPercent
// Top user agents
type kv struct {
Key string
Value int64
}
var uaSlice []kv
for k, v := range adminMetrics.userAgents {
uaSlice = append(uaSlice, kv{k, v})
}
adminMetrics.RUnlock()
// Sort and get top 10
if len(uaSlice) > 10 {
// Simple bubble sort for top 10
for i := 0; i < 10 && i < len(uaSlice); i++ {
for j := i + 1; j < len(uaSlice); j++ {
if uaSlice[j].Value > uaSlice[i].Value {
uaSlice[i], uaSlice[j] = uaSlice[j], uaSlice[i]
}
}
}
uaSlice = uaSlice[:10]
}
for _, ua := range uaSlice {
data.TopUserAgents = append(data.TopUserAgents, UACount{
UserAgent: ua.Key,
Count: ua.Value,
})
}
// Recent errors (last 20)
adminMetrics.RLock()
if len(adminMetrics.errorLog) > 20 {
data.RecentErrors = adminMetrics.errorLog[len(adminMetrics.errorLog)-20:]
} else {
data.RecentErrors = adminMetrics.errorLog
}
adminMetrics.RUnlock()
// Render template
tmpl := template.Must(template.ParseFiles("templates/admin.html"))
if err := tmpl.Execute(w, data); err != nil {
http.Error(w, "Error rendering admin dashboard", http.StatusInternalServerError)
log.Printf("Error rendering admin.html: %v", err)
}
}
// Admin API endpoint for JSON metrics
func adminAPIHandler(w http.ResponseWriter, r *http.Request) {
if !adminAccessAllowed(r) {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
data := map[string]interface{}{
"uptime": time.Since(adminMetrics.startTime).Seconds(),
"total_visitors": getMetricValue(totalVisitors),
"total_lookups": getMetricValue(totalLookups),
"total_leagues": getMetricValue(totalLeagues),
"total_teams": getMetricValue(totalTeams),
"total_errors": getMetricValue(totalErrors),
"goroutines": runtime.NumGoroutine(),
"dynasty_percent": adminMetrics.dynastyPercent,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}
// Helper functions
func adminAccessAllowed(r *http.Request) bool {
adminKey := os.Getenv("ADMIN_KEY")
if adminKey == "" {
return false
}
if adminKey == "changeme" && os.Getenv("ADMIN_ALLOW_INSECURE") != "1" {
return false
}
if !adminIPAllowed(r.RemoteAddr) {
return false
}
providedKey, fromQuery := adminKeyFromRequest(r)
if providedKey == "" {
return false
}
if fromQuery && !allowQueryAuth(r.RemoteAddr) {
return false
}
return providedKey == adminKey
}
func adminKeyFromRequest(r *http.Request) (string, bool) {
auth := strings.TrimSpace(r.Header.Get("Authorization"))
if strings.HasPrefix(auth, "Bearer ") {
return strings.TrimSpace(strings.TrimPrefix(auth, "Bearer ")), false
}
if key := strings.TrimSpace(r.Header.Get("X-Admin-Key")); key != "" {
return key, false
}
if key := strings.TrimSpace(r.URL.Query().Get("secret")); key != "" {
return key, true
}
return "", false
}
func allowQueryAuth(remoteAddr string) bool {
if os.Getenv("ADMIN_ALLOW_QUERY") == "1" {
return true
}
return isLoopbackAddr(remoteAddr)
}
func adminIPAllowed(remoteAddr string) bool {
allowed := strings.TrimSpace(os.Getenv("ADMIN_ALLOWED_IPS"))
if allowed == "" {
return true
}
host, _, err := net.SplitHostPort(remoteAddr)
if err != nil {
host = remoteAddr
}
ip := net.ParseIP(strings.TrimSpace(host))
if ip == nil {
return false
}
for _, entry := range strings.Split(allowed, ",") {
entry = strings.TrimSpace(entry)
if entry == "" {
continue
}
if ip.Equal(net.ParseIP(entry)) {
return true
}
}
return false
}
func isLoopbackAddr(remoteAddr string) bool {
host, _, err := net.SplitHostPort(remoteAddr)
if err != nil {
host = remoteAddr
}
ip := net.ParseIP(strings.TrimSpace(host))
if ip == nil {
return false
}
return ip.IsLoopback()
}
func getMetricValue(counter prometheus.Counter) float64 {
metric := &dto.Metric{}
if err := counter.Write(metric); err != nil {
return 0
}
return metric.Counter.GetValue()
}
func getMemoryUsage() string {
var m runtime.MemStats
runtime.ReadMemStats(&m)
return fmt.Sprintf("%.2f MB", float64(m.Alloc)/1024/1024)
}
// Track page views for admin dashboard
func trackPageView(path string) {
adminMetrics.Lock()
adminMetrics.pageViews[path]++
adminMetrics.Unlock()
}
// Track user agent for admin dashboard
func trackUserAgent(ua string) {
if ua == "" {
return
}
// Simplify user agent (just browser/OS)
simplified := simplifyUserAgent(ua)
adminMetrics.Lock()
adminMetrics.userAgents[simplified]++
adminMetrics.Unlock()
}
// Log error for admin dashboard
func logAdminError(path string, err error, ua string) {
adminMetrics.Lock()
defer adminMetrics.Unlock()
errorLog := ErrorLog{
Timestamp: time.Now(),
Path: path,
Error: err.Error(),
UserAgent: simplifyUserAgent(ua),
}
adminMetrics.errorLog = append(adminMetrics.errorLog, errorLog)
// Keep only last 100 errors
if len(adminMetrics.errorLog) > 100 {
adminMetrics.errorLog = adminMetrics.errorLog[1:]
}
}
// Simplify user agent string
func simplifyUserAgent(ua string) string {
// Extract browser and OS
if ua == "" {
return "Unknown"
}
// Basic parsing - just get the main browser
if contains(ua, "Chrome") {
return "Chrome"
} else if contains(ua, "Safari") && !contains(ua, "Chrome") {
return "Safari"
} else if contains(ua, "Firefox") {
return "Firefox"
} else if contains(ua, "Edge") {
return "Edge"
} else if contains(ua, "Opera") {
return "Opera"
}
// Check for mobile
if contains(ua, "Mobile") {
return "Mobile Browser"
}
return "Other"
}
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(s) > len(substr) && (s[:len(substr)] == substr || s[len(s)-len(substr):] == substr || containsMiddle(s, substr)))
}
func containsMiddle(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}