forked from vulcand/vulcand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
164 lines (150 loc) · 3.25 KB
/
json.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
package backend
import (
"encoding/json"
"fmt"
"github.com/mailgun/vulcand/plugin"
)
type rawHosts struct {
Hosts []json.RawMessage
}
type rawHost struct {
Name string
Locations []json.RawMessage
}
type rawLocation struct {
Hostname string
Path string
Id string
Upstream *Upstream
Middlewares []json.RawMessage
Options LocationOptions
}
type rawMiddleware struct {
Id string
Type string
Priority int
Middleware json.RawMessage
}
func HostsFromJSON(in []byte, getter plugin.SpecGetter) ([]*Host, error) {
var hs *rawHosts
err := json.Unmarshal(in, &hs)
if err != nil {
return nil, err
}
out := []*Host{}
if hs.Hosts != nil {
for _, raw := range hs.Hosts {
h, err := HostFromJSON(raw, getter)
if err != nil {
return nil, err
}
out = append(out, h)
}
}
return out, nil
}
func HostFromJSON(in []byte, getter plugin.SpecGetter) (*Host, error) {
var h *rawHost
err := json.Unmarshal(in, &h)
if err != nil {
return nil, err
}
out, err := NewHost(h.Name)
if err != nil {
return nil, err
}
if h.Locations != nil {
for _, raw := range h.Locations {
l, err := LocationFromJSON(raw, getter)
if err != nil {
return nil, err
}
out.Locations = append(out.Locations, l)
}
}
return out, nil
}
func ListenerFromJSON(in []byte) (*Listener, error) {
var l *Listener
err := json.Unmarshal(in, &l)
if err != nil {
return nil, err
}
return NewListener(l.Id, l.Protocol, l.Address.Network, l.Address.Address)
}
func KeyPairFromJSON(in []byte) (*KeyPair, error) {
var c *KeyPair
err := json.Unmarshal(in, &c)
if err != nil {
return nil, err
}
return NewKeyPair(c.Cert, c.Key)
}
func LocationFromJSON(in []byte, getter plugin.SpecGetter) (*Location, error) {
var l *rawLocation
err := json.Unmarshal(in, &l)
if err != nil {
return nil, err
}
loc, err := NewLocationWithOptions(l.Hostname, l.Id, l.Path, l.Upstream.Id, l.Options)
if err != nil {
return nil, err
}
loc.Upstream = l.Upstream
for _, el := range l.Middlewares {
m, err := MiddlewareFromJSON(el, getter)
if err != nil {
return nil, err
}
loc.Middlewares = append(loc.Middlewares, m)
}
return loc, nil
}
func LocationOptionsFromJSON(in []byte) (*LocationOptions, error) {
var o *LocationOptions
err := json.Unmarshal(in, &o)
if err != nil {
return nil, err
}
if _, err := parseLocationOptions(*o); err != nil {
return nil, err
}
return o, nil
}
func MiddlewareFromJSON(in []byte, getter plugin.SpecGetter) (*MiddlewareInstance, error) {
var ms *rawMiddleware
err := json.Unmarshal(in, &ms)
if err != nil {
return nil, err
}
spec := getter(ms.Type)
if spec == nil {
return nil, fmt.Errorf("middleware of type %s is not supported", ms.Type)
}
m, err := spec.FromJSON(ms.Middleware)
if err != nil {
return nil, err
}
return &MiddlewareInstance{
Id: ms.Id,
Type: ms.Type,
Middleware: m,
Priority: ms.Priority,
}, nil
}
func UpstreamFromJSON(in []byte) (*Upstream, error) {
var u *Upstream
err := json.Unmarshal(in, &u)
if err != nil {
return nil, err
}
return u, nil
}
func EndpointFromJSON(in []byte) (*Endpoint, error) {
var e *Endpoint
err := json.Unmarshal(in, &e)
if err != nil {
return nil, err
}
return NewEndpoint(e.UpstreamId, e.Id, e.Url)
}