-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhandlers.go
90 lines (72 loc) · 1.79 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
auth "github.com/abbot/go-http-auth"
"log"
"net/http"
"strconv"
"time"
)
func indexHandler(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
fmt.Fprint(w, indexHtml)
}
func ServerActionsHandler(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
r.ParseForm()
clientToken, ok := r.PostForm["token"]
if ok == false {
fmt.Fprint(w, "{\"error\": \"token wasn't specified\"}")
return
}
if clientToken[0] != serverToken {
fmt.Fprint(w, "{\"error\": \"wrong token\"}")
return
}
clientAction, ok := r.PostForm["action"]
if ok == false {
fmt.Fprint(w, "{\"error\": \"action wasn't specified\"}")
return
}
params := make(map[string]string)
params["char_id"] = r.PostFormValue("params[char_id]")
log.Println(params)
processClientAction(clientAction[0], w, params)
}
func ServerStatusHandler(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
r.ParseForm()
clientToken, ok := r.PostForm["token"]
if ok == false {
fmt.Fprint(w, "{\"error\": \"token wasn't specified\"}")
return
}
if clientToken[0] != serverToken {
fmt.Fprint(w, "{\"error\": \"wrong token\"}")
return
}
clientTopicVersion, ok := r.PostForm["topic_version"]
clientTopicVersionInt, err := strconv.Atoi(clientTopicVersion[0])
if err != nil {
clientTopicVersionInt = 0
}
if ok && clientTopicVersionInt != topicVersion {
response := getStatusResponse(true)
fmt.Fprint(w, response)
return
}
timeout := make(chan bool)
responseChan := make(chan string)
pollIndex := len(statusPolls)
statusPolls[pollIndex] = responseChan
go func() {
time.Sleep(time.Second * 35)
timeout <- true
}()
select {
case responseBody := <-responseChan:
fmt.Fprint(w, responseBody)
return
case <-timeout:
delete(statusPolls, pollIndex)
w.WriteHeader(503)
return
}
}