-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTPRouter.go
214 lines (183 loc) · 5.98 KB
/
HTTPRouter.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"net/http"
"github.com/sirupsen/logrus"
"encoding/json"
"github.com/gorilla/mux"
)
func withTokenAndDB(db *GeeoDB, wsh *WSRouter, fn func(http.ResponseWriter, *http.Request, *JWTToken, *GeeoDB, *WSRouter)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
t := req.Header.Get("X-GEEO-TOKEN")
if t == "" {
t = req.URL.Query().Get("token")
}
token, err := parseJWTToken(t)
if err != nil || !token.Capabilities.HTTP {
message := struct {
Error string `json:"error"`
Message string `json:"message"`
}{"Can't parse token, or token invalid", err.Error()}
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode(message)
log.Warn("HTTP route: can't parse token, or token without HTTP cap")
return
}
fn(w, req, token, db, wsh)
}
}
// NewHTTPRouter returns the router for the geeo http api
func NewHTTPRouter(router *mux.Router, db *GeeoDB, wsh *WSRouter) *mux.Router {
router.HandleFunc("/v1/ping", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-type", "application/json")
w.WriteHeader(200)
json.NewEncoder(w).Encode(struct {
Tag string
Build string
}{Tag, Build})
})
router.HandleFunc("/v1/POI", withTokenAndDB(db, wsh, addRemovePOI))
router.HandleFunc("/v1/airbeacon", withTokenAndDB(db, wsh, addRemoveAirBeacon))
router.HandleFunc("/v1/log", setLogLevel) // doesn't need additional security, awaits bearer token
return router
}
func addRemovePOI(w http.ResponseWriter, req *http.Request, token *JWTToken, db *GeeoDB, wsh *WSRouter) {
w.Header().Set("Content-type", "application/json")
if !token.Capabilities.POI {
w.WriteHeader(http.StatusForbidden)
json.NewEncoder(w).Encode(struct {
Error string
}{"Your token doesn't allow POI creation/removal"})
log.Warn("POI HTTP route: Your token doesn't allow POI creation/removal")
return
}
if req.Method != http.MethodPost && req.Method != http.MethodDelete {
w.WriteHeader(http.StatusMethodNotAllowed)
json.NewEncoder(w).Encode(struct {
Error string
}{"Only POST and DELETE are supported by this endpoint"})
log.Warn("POI HTTP route: Only POST and DELETE are supported by this endpoint")
return
}
cmd := &JSONPOI{}
err := json.NewDecoder(req.Body).Decode(cmd)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(struct {
Error string
}{"Can't parse json body"})
log.Warn("POI HTTP route: Can't parse json body")
return
}
log.Debug("POI HTTP command: ", cmd)
switch req.Method {
case http.MethodPost:
log.Info("POST /v1/POI: ", *cmd.ID, " created by ", cmd.Creator, " at ", cmd.Pos)
poi := db.addPOI(*cmd.ID, cmd.Pos, cmd.PublicData, cmd.Creator)
go func() {
message := poi.enterLeaveMessage(true)
wsh.sendMessageToConsumersWithPoint(message, poi.GetPoint())
}()
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(*poi)
case http.MethodDelete:
log.Info("DELETE /v1/POI: ", *cmd.ID)
db.RLock()
poi, found := db.pois[*cmd.ID]
db.RUnlock()
if !found {
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(struct {
Error string
}{"POI not found"})
log.Warn("POI HTTP route: POI not found")
return
}
db.removePOI(poi)
go func() {
message := poi.enterLeaveMessage(false)
wsh.sendMessageToConsumersWithPoint(message, poi.GetPoint())
}()
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(*poi)
}
}
func addRemoveAirBeacon(w http.ResponseWriter, req *http.Request, token *JWTToken, db *GeeoDB, wsh *WSRouter) {
w.Header().Set("Content-type", "application/json")
if !token.Capabilities.AirBeacon {
json.NewEncoder(w).Encode(struct {
Error string
}{"Your token doesn't allow Air Beacon creation/removal"})
log.Warn("AirBeacon HTTP route: Your token doesn't allow Air Beacon creation/removal")
return
}
if req.Method != http.MethodPost && req.Method != http.MethodDelete {
w.WriteHeader(http.StatusMethodNotAllowed)
json.NewEncoder(w).Encode(struct {
Error string
}{"Only POST and DELETE are supported by this endpoint"})
log.Warn("AirBeacon HTTP route: Only POST and DELETE are supported by this endpoint")
return
}
cmd := &JSONAirBeacon{}
err := json.NewDecoder(req.Body).Decode(cmd)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(struct {
Error string
}{"Can't parse json body"})
log.Warn("AirBeacon HTTP route: Can't parse json body")
return
}
log.Debug("Airbeacon HTTP command: ", cmd)
switch req.Method {
case http.MethodPost:
log.Info("POST /v1/airbeacon: ", *cmd.ID, " created by ", cmd.Creator, " at ", cmd.Pos)
poi := db.addAirBeacon(*cmd.ID, cmd.Pos, cmd.PublicData, cmd.Creator)
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(*poi)
case http.MethodDelete:
log.Info("DELETE /v1/airbeacon: ", *cmd.ID)
db.RLock()
ab, found := db.ab[*cmd.ID]
db.RUnlock()
if !found {
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(struct {
Error string
}{"AirBeacon not found"})
log.Warn("AirBeacon HTTP route: AirBeacon not found")
return
}
db.removeAirBeacon(*ab.id)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(*ab)
}
}
func setLogLevel(w http.ResponseWriter, req *http.Request) {
auth := req.Header.Get("Authorization")
if WebhookBearerToken == "" || (auth != WebhookBearerToken && req.URL.Query().Get("bearer") != WebhookBearerToken) {
w.WriteHeader(http.StatusUnauthorized)
log.Warn("Unauthorized attempt to set log level")
return
}
if req.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
log.Warn("Wrong HTTP method to set log level")
return
}
level := req.URL.Query().Get("level")
log.Info("Setting log level to ", level)
switch level {
case "debug":
log.SetLevel(logrus.DebugLevel)
case "info":
log.SetLevel(logrus.InfoLevel)
case "warn":
log.SetLevel(logrus.WarnLevel)
case "error":
log.SetLevel(logrus.ErrorLevel)
default:
log.Warn("Invalid log level, ", level)
}
w.WriteHeader(http.StatusOK)
}