/
handlers.go
169 lines (163 loc) · 5.51 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
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
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"log"
"net/http"
"time"
)
// APIHandler returns a http.Handler that matches URLs of the latest API.
func APIHandler() http.Handler {
// API v1
t := time.Now()
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "API OK")
})
r.HandleFunc("/uptime", func(w http.ResponseWriter, r *http.Request) {
d := time.Since(t)
var h, m, s int = int(d.Hours()), int(d.Minutes()), int(d.Seconds())
fmt.Fprintf(w, "API uptime: %dd%02dh%02dm%02ds\n", h/24, h%24, m%60, s%60)
})
s := r.PathPrefix("/1").Subrouter()
for pattern, handler := range map[string]contextualHandlerFunc{
"/installation/new": NewInstallationHandler,
"/session/new": NewSessionHandler,
"/session/close": CloseSessionHandler,
"/session/ping": PingSessionHandler,
} {
s.Handle(pattern, handler).Methods("POST")
}
return r
}
// NewInstallationHandler ...
func NewInstallationHandler(w http.ResponseWriter, r *http.Request, c *Context) {
machineId := r.PostFormValue("machine_id")
xmppvoxVersion := r.PostFormValue("xmppvox_version")
dosvoxInfoStr := r.PostFormValue("dosvox_info")
machineInfoStr := r.PostFormValue("machine_info")
if len(r.PostForm) != 4 || machineId == "" || xmppvoxVersion == "" || dosvoxInfoStr == "" || machineInfoStr == "" {
http.Error(w, "Retry with POST parameters: machine_id, xmppvox_version, dosvox_info, machine_info",
http.StatusBadRequest)
return
}
var dosvoxInfo, machineInfo map[string]string
err := json.Unmarshal([]byte(dosvoxInfoStr), &dosvoxInfo)
if err != nil {
http.Error(w, "Invalid JSON for dosvox_info", http.StatusBadRequest)
return
}
err = json.Unmarshal([]byte(machineInfoStr), &machineInfo)
if err != nil {
http.Error(w, "Invalid JSON for machine_info", http.StatusBadRequest)
return
}
i := NewInstallation(machineId, xmppvoxVersion, dosvoxInfo, machineInfo)
err = c.Store.InsertInstallation(i)
if mgo.IsDup(err) {
http.Error(w, "Installation already registered", http.StatusBadRequest)
return
}
switch err {
case nil:
fmt.Fprintln(w, machineId)
default:
http.Error(w, fmt.Sprintf("Failed to track install %s", machineId),
http.StatusInternalServerError)
log.Println(err)
}
}
// NewSessionHandler ...
func NewSessionHandler(w http.ResponseWriter, r *http.Request, c *Context) {
jid := r.PostFormValue("jid")
machineId := r.PostFormValue("machine_id")
xmppvoxVersion := r.PostFormValue("xmppvox_version")
if len(r.PostForm) != 3 || jid == "" || machineId == "" || xmppvoxVersion == "" {
http.Error(w, "Retry with POST parameters: jid, machine_id, xmppvox_version", http.StatusBadRequest)
return
}
// A new session might be forbidden under certain conditions,
// such as xmppvoxVersion, machineId or jid.
// The client will stop executing and display a message to the user.
//if ... {
// http.Error(w, "DENY SESSION WITH A MESSAGE", http.StatusForbidden)
// return
//}
s := NewSession(jid, machineId, xmppvoxVersion, &HttpRequest{
Method: r.Method,
URL: r.URL,
Header: r.Header,
Host: r.Host,
Form: r.Form,
RemoteAddr: r.RemoteAddr,
})
err := c.Store.InsertSession(s)
switch err {
case nil:
fmt.Fprintln(w, s.Id.Hex())
// Together with a sessionId, the response body might include a message.
// The client will display the message to the user right after acquiring
// the sessionId.
//if ... {
// fmt.Fprintln(w, "APPEND A MESSAGE TO XMPPVOX")
//}
default:
http.Error(w, "Failed to create a new session", http.StatusInternalServerError)
log.Println(err)
}
}
// CloseSessionHandler ...
func CloseSessionHandler(w http.ResponseWriter, r *http.Request, c *Context) {
sessionIdHex := r.PostFormValue("session_id")
machineId := r.PostFormValue("machine_id")
if len(r.PostForm) != 2 || sessionIdHex == "" || machineId == "" {
http.Error(w, "Retry with POST parameters: session_id, machine_id", http.StatusBadRequest)
return
}
if !bson.IsObjectIdHex(sessionIdHex) {
http.Error(w, fmt.Sprintf("Invalid session id %s", sessionIdHex), http.StatusBadRequest)
return
}
sessionId := bson.ObjectIdHex(sessionIdHex)
err := c.Store.CloseSession(&Session{Id: sessionId, MachineId: machineId})
switch err {
case nil:
fmt.Fprintln(w, sessionIdHex)
case mgo.ErrNotFound:
http.Error(w, fmt.Sprintf("Session %s does not exist or is already closed", sessionIdHex),
http.StatusBadRequest)
default:
http.Error(w, fmt.Sprintf("Failed to close session %s", sessionIdHex),
http.StatusInternalServerError)
log.Println(err)
}
}
// PingSessionHandler ...
func PingSessionHandler(w http.ResponseWriter, r *http.Request, c *Context) {
sessionIdHex := r.PostFormValue("session_id")
machineId := r.PostFormValue("machine_id")
if len(r.PostForm) != 2 || sessionIdHex == "" || machineId == "" {
http.Error(w, "Retry with POST parameters: session_id, machine_id", http.StatusBadRequest)
return
}
if !bson.IsObjectIdHex(sessionIdHex) {
http.Error(w, fmt.Sprintf("Invalid session id %s", sessionIdHex), http.StatusBadRequest)
return
}
sessionId := bson.ObjectIdHex(sessionIdHex)
err := c.Store.PingSession(&Session{Id: sessionId, MachineId: machineId})
switch err {
case nil:
fmt.Fprintln(w, sessionIdHex)
case mgo.ErrNotFound:
http.Error(w, fmt.Sprintf("Session %s does not exist or is already closed", sessionIdHex),
http.StatusBadRequest)
default:
http.Error(w, fmt.Sprintf("Failed to ping session %s", sessionIdHex),
http.StatusInternalServerError)
log.Println(err)
}
}