This repository has been 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
/
http.go
117 lines (111 loc) · 3.63 KB
/
http.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
package swgohapi
import (
"encoding/json"
"net/http"
"net/url"
"strings"
"time"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"google.golang.org/appengine/taskqueue"
)
func init() {
http.HandleFunc("/v1/profile/", ProfileHandler)
http.HandleFunc("/admin/reloadAll", ReloadAll)
}
// ProfileHandler renders the profile or cache the profile if the fullUpdate
// parameter is provided.
func ProfileHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
user := strings.Replace(r.URL.Path, "/v1/profile/", "", -1)
if user == "" {
log.Infof(c, "Invalid profile: %v", user)
http.Error(w, "Invalid profile: "+user, http.StatusBadRequest)
return
}
// TODO: use always lower case username - normalizes a lot of bugs.
if unescaped, err := url.QueryUnescape(user); err == nil {
user = unescaped
}
// For simplicity, if we are told to reaload, just parse the whole
// data from site and save again.
fullUpdate := r.FormValue("fullUpdate") == "true"
if fullUpdate {
log.Infof(c, "Reloading profile (fullUpdate=true)")
_, err := ReloadProfile(c, user, fullUpdate)
if err != nil {
log.Warningf(c, "Unable to reload profile: %v", err)
return
}
log.Infof(c, "Profile reloaded")
}
// Lookup the cached profile...
p, err := GetProfile(c, user)
// ... if failure, report
if err != nil {
log.Errorf(c, "Error loading profile: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
// ... if not found, schedule
if p == nil {
if err = ReloadProfileAsync(c, user, false); err != nil {
log.Errorf(c, "Error loading scheduling profile sync: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Error(w, "{\"Status\": \"Reloading\"}", http.StatusAccepted)
return
}
if r.Header.Get("X-AppEngine-TaskName") != "" {
log.Infof(c, "Running from Task Queue, not rendering response payload.")
w.Write([]byte("OK"))
return
}
// ... render the API response if we get a valid, cached data.
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "public, max-age=3600")
if err = json.NewEncoder(w).Encode(p); err != nil {
log.Errorf(c, "Error encoding profile: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// ReloadAll fore-reload all expired cache data.
func ReloadAll(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
log.Infof(c, "Running schedule all routine ... ")
q := datastore.NewQuery(PlayerDataKind).
Filter("LastUpdate <", time.Now().Add(-24*time.Hour)).
KeysOnly()
expired, err := q.GetAll(c, nil)
if err != nil {
log.Errorf(c, "Error loading expired profiles: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Infof(c, "Found %d expired profiles", len(expired))
tasks := make([]*taskqueue.Task, 0)
for _, key := range expired {
escapedProfile := url.QueryEscape(key.StringID())
escapedProfile = strings.Replace(escapedProfile, "+", "%20", -1)
tasks = append(tasks, taskqueue.NewPOSTTask("/v1/profile/"+escapedProfile, url.Values{
"fullUpdate": {"true"},
}))
log.Debugf(c, "Added task for %s", escapedProfile)
if len(tasks) > 10 {
log.Infof(c, "Scheduling profiles %v", tasks)
if _, err := taskqueue.AddMulti(c, tasks, "sync"); err != nil {
log.Warningf(c, "Error scheduling: %v", err)
}
tasks = make([]*taskqueue.Task, 0)
}
}
if len(tasks) > 0 {
log.Infof(c, "Scheduling profiles %v", tasks)
if _, err := taskqueue.AddMulti(c, tasks, "sync"); err != nil {
log.Warningf(c, "Error scheduling: %v", err)
}
}
log.Infof(c, "Done")
}