/
handlers.go
258 lines (211 loc) · 6.05 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package http
import (
"encoding/json"
"net/http"
m "github.com/antoineaugusti/feature-flags/models"
services "github.com/antoineaugusti/feature-flags/services"
"github.com/gorilla/mux"
)
// Handles incoming requests
type APIHandler struct {
FeatureService services.FeatureService
}
// A simple structure to respond with error messages
type APIMessage struct {
// The HTTP status code
code int
// A status message
Status string `json:"status"`
// A human readable message
Message string `json:"message"`
}
// Describes the request when checking the access to a feature
type AccessRequest struct {
Groups []string `json:"groups"`
User uint32 `json:"user"`
}
func (handler APIHandler) FeatureIndex(w http.ResponseWriter, r *http.Request) {
features, err := handler.FeatureService.GetFeatures()
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", getJsonHeader())
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(features); err != nil {
panic(err)
}
}
func (handler APIHandler) FeatureShow(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
// Check if the feature exists
if !handler.featureExists(vars["featureKey"]) {
writeNotFound(w)
return
}
// Fetch the feature
feature, err := handler.FeatureService.GetFeature(vars["featureKey"])
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", getJsonHeader())
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(feature); err != nil {
panic(err)
}
}
func (handler APIHandler) FeaturesAccess(w http.ResponseWriter, r *http.Request) {
var ar AccessRequest
// Get all features in the bucket
features, err := handler.FeatureService.GetFeatures()
if err != nil {
panic(err)
}
// Decode the access request
err = json.NewDecoder(r.Body).Decode(&ar)
if err != nil {
writeUnprocessableEntity(err, w)
return
}
// Keep only accessible features
accessibleFeatures := make(m.FeatureFlags, 0)
for _, feature := range features {
if hasAccessToFeature(feature, ar) {
accessibleFeatures = append(accessibleFeatures, feature)
}
}
w.Header().Set("Content-Type", getJsonHeader())
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(accessibleFeatures); err != nil {
panic(err)
}
}
func (handler APIHandler) FeatureAccess(w http.ResponseWriter, r *http.Request) {
var ar AccessRequest
vars := mux.Vars(r)
// Check if the feature exists
if !handler.featureExists(vars["featureKey"]) {
writeNotFound(w)
return
}
// Fetch the feature
feature, err := handler.FeatureService.GetFeature(vars["featureKey"])
if err != nil {
panic(err)
}
// Decode the access request
err = json.NewDecoder(r.Body).Decode(&ar)
if err != nil {
writeUnprocessableEntity(err, w)
return
}
if hasAccessToFeature(feature, ar) {
writeMessage(http.StatusOK, "has_access", "The user has access to the feature", w)
} else {
writeMessage(http.StatusOK, "not_access", "The user does not have access to the feature", w)
}
}
func (handler APIHandler) FeatureRemove(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
// Check if the feature exists
if !handler.featureExists(vars["featureKey"]) {
writeNotFound(w)
return
}
// Delete it
err := handler.FeatureService.RemoveFeature(vars["featureKey"])
if err != nil {
panic(err)
}
writeMessage(http.StatusOK, "feature_deleted", "The feature was successfully deleted", w)
}
func (handler APIHandler) FeatureCreate(w http.ResponseWriter, r *http.Request) {
var feature m.FeatureFlag
if err := json.NewDecoder(r.Body).Decode(&feature); err != nil {
writeUnprocessableEntity(err, w)
return
}
if err := feature.Validate(); err != nil {
writeMessage(400, "invalid_feature", err.Error(), w)
return
}
err := handler.FeatureService.AddFeature(feature)
if err != nil && err.Error() == "Feature already exists" {
writeMessage(400, "invalid_feature", err.Error(), w)
return
}
w.Header().Set("Content-Type", getJsonHeader())
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(feature); err != nil {
panic(err)
}
}
func (handler APIHandler) FeatureEdit(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
// Check if the feature exists
if !handler.featureExists(vars["featureKey"]) {
writeNotFound(w)
return
}
// Fetch the feature
newFeature, err := handler.FeatureService.GetFeature(vars["featureKey"])
if err != nil {
panic(err)
}
// Update the overwritten fields of the feature
if err = json.NewDecoder(r.Body).Decode(&newFeature); err != nil {
writeUnprocessableEntity(err, w)
return
}
// Validate given values
if err := newFeature.Validate(); err != nil {
writeMessage(400, "invalid_feature", err.Error(), w)
return
}
newFeature, err = handler.FeatureService.UpdateFeature(vars["featureKey"], newFeature)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", getJsonHeader())
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(newFeature); err != nil {
panic(err)
}
}
func (handler APIHandler) featureExists(featureKey string) bool {
return handler.FeatureService.FeatureExists(featureKey)
}
func getJsonHeader() string {
return "application/json"
}
func writeNotFound(w http.ResponseWriter) {
writeMessage(http.StatusNotFound, "feature_not_found", "The feature was not found", w)
}
func writeUnprocessableEntity(err error, w http.ResponseWriter) {
writeMessage(422, "invalid_json", "Cannot decode the given JSON payload", w)
}
func writeMessage(code int, status string, message string, w http.ResponseWriter) {
apiMessage := APIMessage{code, status, message}
bytes, _ := json.Marshal(apiMessage)
w.Header().Set("Content-Type", getJsonHeader())
w.WriteHeader(apiMessage.code)
w.Write(bytes)
}
func hasAccessToFeature(feature m.FeatureFlag, ar AccessRequest) bool {
// Handle trivial case
if feature.IsEnabled() {
return true
}
// Access thanks to a group?
if len(ar.Groups) > 0 {
for _, group := range ar.Groups {
if feature.GroupHasAccess(group) {
return true
}
}
}
// Access thanks to the user?
if ar.User > 0 {
return feature.UserHasAccess(ar.User)
}
return false
}