-
Notifications
You must be signed in to change notification settings - Fork 50
/
handler.go
118 lines (99 loc) · 3.3 KB
/
handler.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
package ofrep
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/open-feature/flagd/core/pkg/evaluator"
"github.com/open-feature/flagd/core/pkg/logger"
"github.com/open-feature/flagd/core/pkg/service/ofrep"
"github.com/rs/xid"
)
const (
key = "key"
singleEvaluation = "/ofrep/v1/evaluate/flags/{key}"
bulkEvaluation = "/ofrep/v1/evaluate/{path:flags\\/|flags}"
)
type handler struct {
Logger *logger.Logger
evaluator evaluator.IEvaluator
}
func NewOfrepHandler(logger *logger.Logger, evaluator evaluator.IEvaluator) http.Handler {
h := handler{
logger,
evaluator,
}
router := mux.NewRouter()
router.HandleFunc(singleEvaluation, h.HandleFlagEvaluation).Methods("POST")
router.HandleFunc(bulkEvaluation, h.HandleBulkEvaluation).Methods("POST")
return router
}
func (h *handler) HandleFlagEvaluation(w http.ResponseWriter, r *http.Request) {
requestID := xid.New().String()
// obtain flag key
vars := mux.Vars(r)
if vars == nil {
h.writeJSONToResponse(
http.StatusInternalServerError,
ofrep.InternalError{ErrorDetails: "failed to obtain the flag key from the request"}, w)
return
}
flagKey := vars[key]
request, err := extractOfrepRequest(r)
if err != nil {
h.writeJSONToResponse(http.StatusBadRequest, ofrep.ContextErrorResponseFrom(flagKey), w)
return
}
context := flagdContext(h.Logger, requestID, request)
evaluation := h.evaluator.ResolveAsAnyValue(r.Context(), requestID, flagKey, context)
if evaluation.Error != nil {
status, evaluationError := ofrep.EvaluationErrorResponseFrom(evaluation)
h.writeJSONToResponse(status, evaluationError, w)
} else {
h.writeJSONToResponse(http.StatusOK, ofrep.SuccessResponseFrom(evaluation), w)
}
}
func (h *handler) HandleBulkEvaluation(w http.ResponseWriter, r *http.Request) {
requestID := xid.New().String()
request, err := extractOfrepRequest(r)
if err != nil {
h.writeJSONToResponse(http.StatusBadRequest, ofrep.BulkEvaluationContextErrorFrom(), w)
return
}
context := flagdContext(h.Logger, requestID, request)
evaluations := h.evaluator.ResolveAllValues(r.Context(), requestID, context)
h.writeJSONToResponse(http.StatusOK, ofrep.BulkEvaluationResponseFrom(evaluations), w)
}
func (h *handler) writeJSONToResponse(status int, payload interface{}, w http.ResponseWriter) {
// first marshal payload
marshal, err := json.Marshal(payload)
if err != nil {
// always a 500
h.Logger.Warn(fmt.Sprintf("error marshelling the response: %v", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(status)
_, err = w.Write(marshal)
if err != nil {
h.Logger.Warn(fmt.Sprintf("error while writing response: %v", err))
}
}
func extractOfrepRequest(req *http.Request) (ofrep.Request, error) {
request := ofrep.Request{}
err := json.NewDecoder(req.Body).Decode(&request)
if err != nil && err.Error() != "EOF" {
return request, fmt.Errorf("decode error: %w", err)
}
return request, nil
}
func flagdContext(log *logger.Logger, requestID string, request ofrep.Request) map[string]any {
context := map[string]any{}
if res, ok := request.Context.(map[string]any); ok {
context = res
} else {
log.WarnWithID(requestID, "provided context does not comply with flagd, continuing ignoring the context")
}
return context
}