forked from Flyclops/GoCM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
71 lines (60 loc) · 1.61 KB
/
handlers.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
package main
import (
"encoding/json"
"io"
"net/http"
"strconv"
)
// Send a message to GCM
func send(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
tokens := r.PostForm["tokens"]
payload := r.PostFormValue("payload")
go func() {
incrementPending()
sendMessageToGCM(tokens, payload)
}()
// Return immediately
output := "ok\n"
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Content-Length", strconv.Itoa(len(output)))
io.WriteString(w, output)
}
// Return a run report for this process
func getReport(w http.ResponseWriter, r *http.Request) {
runReportMutex.Lock()
a, _ := json.Marshal(runReport)
runReportMutex.Unlock()
b := string(a)
io.WriteString(w, b)
}
// Return all currently collected canonical reports from GCM
func getCanonicalReport(w http.ResponseWriter, r *http.Request) {
canonicalReplacementsMutex.Lock()
ids := map[string][]canonicalReplacement{"canonical_replacements": canonicalReplacements}
a, _ := json.Marshal(ids)
canonicalReplacementsMutex.Unlock()
b := string(a)
io.WriteString(w, b)
// Clear out canonicals
go func() {
canonicalReplacementsMutex.Lock()
defer canonicalReplacementsMutex.Unlock()
canonicalReplacements = nil
}()
}
// Return all tokens that need to be unregistered
func getNotRegisteredReport(w http.ResponseWriter, r *http.Request) {
notRegisteredMutex.Lock()
ids := map[string][]string{"tokens": notRegisteredKeys}
a, _ := json.Marshal(ids)
notRegisteredMutex.Unlock()
b := string(a)
io.WriteString(w, b)
// Clear ids
go func() {
notRegisteredMutex.Lock()
defer notRegisteredMutex.Unlock()
notRegisteredKeys = nil
}()
}