/
handlers.go
170 lines (143 loc) · 4.62 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 handlers
import (
"bouncer/config"
"bouncer/experiment"
"bouncer/feature"
"encoding/json"
"errors"
"fmt"
"github.com/getsentry/raven-go"
"github.com/unrolled/render"
"io/ioutil"
"net/http"
"runtime/debug"
)
type ConfigFactory func () *config.Config
type Context struct {
Uid string `json:"uid"`
}
type ParticipateResponse struct {
Experiments map[string]string `json:"experiments,omitempty"`
Features map[string]bool `json:"features,omitempty"`
}
type ParticipateRequest struct {
Context Context `json:"context"`
Experiments map[string][]string `json:"experiments,omitempty"`
Features map[string]float32 `json:"features,omitempty"`
}
func ErrorHandler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rval := recover(); rval != nil {
rvalStr := fmt.Sprint(rval)
packet := raven.NewPacket(rvalStr,
raven.NewException(errors.New(rvalStr), raven.NewStacktrace(2, 3, nil)),
raven.NewHttp(r))
raven.Capture(packet, nil)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("500 Server Error: %s\n\n", rval)))
w.Write(debug.Stack())
}
}()
handler.ServeHTTP(w, r)
})
}
func Root(w http.ResponseWriter, req *http.Request) {
r := render.New()
r.JSON(w, http.StatusOK,
map[string][]string{
"app": {"bouncer"},
"paths": {"/experiments/", "/features/", "/groups/", "/participate/", "/stats/"},
})
}
func ListExperiments(cfg ConfigFactory) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
r := render.New()
r.JSON(w, http.StatusOK, cfg().Experiments)
}
}
func ListFeatures(cfg ConfigFactory) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
r := render.New()
r.JSON(w, http.StatusOK, cfg().Features)
}
}
func ListGroups(cfg ConfigFactory) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
r := render.New()
r.JSON(w, http.StatusOK, cfg().Groups)
}
}
func CheckFeatures(features map[string]float32, uid string, cfg config.Config) map[string]bool {
r := make(map[string]bool)
if len(features) == 0 {
for featureName, f := range cfg.FeatureMap {
r[featureName] = f.IsEnabled(uid)
}
return r
}
for featureName, _ := range features {
if f, ok := cfg.FeatureMap[featureName]; ok {
r[f.Name] = f.IsEnabled(uid)
} else {
// Un-configured feature specified by the client.
f := feature.NewFeature(featureName, features[featureName], make(map[string]int))
r[f.Name] = f.IsEnabled(uid)
}
}
return r
}
func CheckExperiments(experiments map[string][]string, uid string, cfg config.Config) map[string]string {
r := make(map[string]string)
// if we don't specify experiments to participate in return all
if len(experiments) == 0 {
for experimentName, exp := range cfg.ExperimentMap {
r[experimentName] = exp.GetAlternative(uid)
}
return r
}
// else only return requested experiments
for experimentName, _ := range experiments {
if e, ok := cfg.ExperimentMap[experimentName]; ok {
r[experimentName] = e.GetAlternative(uid)
} else {
// Un-configured feature specified by the client.
alts := make([]experiment.Alternative, len(experiments[experimentName]))
for _, alternativeName := range experiments[experimentName] {
alts = append(alts, experiment.Alternative{alternativeName, 1})
}
e := experiment.NewExperiment(experimentName, make(map[string]string), alts...)
r[experimentName] = e.GetAlternative(uid)
}
}
return r
}
func Participate(config ConfigFactory, logger func (interface{})) func(http.ResponseWriter, *http.Request) {
cfg := *config()
return func(w http.ResponseWriter, req *http.Request) {
r := render.New()
if req.Method != "POST" {
r.JSON(w, http.StatusMethodNotAllowed, "Method not supported")
fmt.Errorf("ERROR: method not supported")
return
}
preq := ParticipateRequest{}
body, rerr := ioutil.ReadAll(req.Body)
if rerr != nil {
r.JSON(w, http.StatusBadRequest, fmt.Sprintf("Error reading body: %s", rerr))
fmt.Errorf("ERROR: reading body: %v", rerr)
return
}
err := json.Unmarshal(body, &preq)
if err != nil {
r.JSON(w, http.StatusBadRequest, fmt.Sprintf("Error decoding json: %s", err))
fmt.Errorf("ERROR: decoding json: %s received: %s", err, string(body))
return
}
presp := new(ParticipateResponse)
presp.Experiments = CheckExperiments(preq.Experiments, preq.Context.Uid, cfg)
presp.Features = CheckFeatures(preq.Features, preq.Context.Uid, cfg)
r.JSON(w, http.StatusOK, presp)
go logger(*presp)
}
}