-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_selector.go
66 lines (53 loc) · 1.63 KB
/
backend_selector.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
package main
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"github.com/google/martian/parse"
)
func init() {
parse.Register("provablyFair.BackendSelector", backendSelectorFromJSON)
}
// BackendSelector substitutes the host of the HTTP request depending on the value of
// the `Game` field of the requesy body
type BackendSelector struct {
Mapping map[string]string
}
// Err404 is the error returned by the modifier if the requested game is not defined
// in the mapping injected to the BackendSelector
var Err404 = errors.New("unnknown game")
// ModifyRequest extracts the value of the `Game` field from the request and updates the
// Host of the request URL using in the injected mapping
func (b *BackendSelector) ModifyRequest(req *http.Request) error {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return err
}
req.Body.Close()
req.Body = ioutil.NopCloser(bytes.NewReader(body))
s := new(Session)
if err := json.Unmarshal(body, s); err != nil {
return err
}
backend, ok := b.Mapping[s.Game]
if !ok {
return Err404
}
req.URL.Host = backend
return nil
}
// Session is struct for quick param extraction from the request body
type Session struct {
Game string `json:"game"`
}
// backendSelectorFromJSON is the factory used by the martian package to instantiate
// the modifier registered under the name "provablyFair.BackendSelector"
func backendSelectorFromJSON(b []byte) (*parse.Result, error) {
mapping := map[string]string{}
if err := json.Unmarshal(b, &mapping); err != nil {
return nil, err
}
return parse.NewResult(&BackendSelector{Mapping: mapping}, []parse.ModifierType{parse.Request})
}