This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.go
More file actions
56 lines (49 loc) · 1.39 KB
/
admin.go
File metadata and controls
56 lines (49 loc) · 1.39 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
package swgohapi
import (
"fmt"
"html/template"
"net/http"
"time"
"golang.org/x/net/context"
"google.golang.org/appengine/log"
"google.golang.org/appengine"
)
func init() {
http.HandleFunc("/_swgoh/admin", adminHandler)
}
func adminHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
values := make(map[string]interface{})
values["Now"] = time.Now().Format(time.RFC3339)
switch r.Method {
case "GET":
stats, err := GetPlayerStats(ctx)
if err != nil {
errorf(ctx, w, "Error loading player stats: %v", err)
return
}
values["Stats"] = stats
values["SinceOldestUpdate"] = time.Since(stats.OldestPlayerSync)
stale, err := ListStalePlayers(ctx)
if err != nil {
errorf(ctx, w, "Error listing stale players: %v", err)
return
}
values["StalePlayers"] = stale
err = renderTemplate(w, "admin.html", values)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func renderTemplate(w http.ResponseWriter, name string, values map[string]interface{}) error {
tpl, err := template.New("base.html").ParseFiles("templates/base.html", "templates/"+name)
if err != nil {
return err
}
return tpl.Execute(w, values)
}
func errorf(ctx context.Context, w http.ResponseWriter, msg string, args ...interface{}) {
log.Warningf(ctx, msg, args...)
http.Error(w, fmt.Sprintf(msg, args...), http.StatusInternalServerError)
}