This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
types.go
92 lines (78 loc) · 1.93 KB
/
types.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
package strowger
import (
"encoding/json"
"errors"
"time"
)
type Route struct {
ID string `json:"id,omitempty"`
ParentRef string `json:"parent_ref,omitempty"`
Type string `json:"type,omitempty"`
Config *json.RawMessage `json:"config,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
var ErrWrongType = errors.New("strowger: the requested route type does not match the actual type")
var ErrNoConfig = errors.New("strowger: the supplied route has no configuration")
func (r *Route) HTTPRoute() *HTTPRoute {
rCopy := *r
if rCopy.Config == nil {
empty := json.RawMessage(`{}`)
rCopy.Config = &empty
}
route := &HTTPRoute{Route: &rCopy}
route.Route.Config = nil
json.Unmarshal(*r.Config, route)
return route
}
func (r *Route) TCPRoute() *TCPRoute {
rCopy := *r
if rCopy.Config == nil {
empty := json.RawMessage(`{}`)
rCopy.Config = &empty
}
route := &TCPRoute{Route: &rCopy}
route.Route.Config = nil
json.Unmarshal(*r.Config, route)
return route
}
type HTTPRoute struct {
*Route `json:"-"`
Domain string `json:"domain,omitempty"`
Service string `json:"service,omitempty"`
TLSCert string `json:"tls_cert,omitempty"`
TLSKey string `json:"tls_key,omitempty"`
Sticky bool `json:"sticky,omitempty"`
}
func (r *HTTPRoute) ToRoute() *Route {
if r.Route == nil {
r.Route = &Route{}
}
r.Route.Type = "http"
config, _ := json.Marshal(r)
jsonConfig := json.RawMessage(config)
route := *r.Route
route.Config = &jsonConfig
return &route
}
type TCPRoute struct {
*Route `json:"-"`
Port int `json:"port"`
Service string `json:"service"`
}
func (r *TCPRoute) ToRoute() *Route {
if r.Route == nil {
r.Route = &Route{}
}
r.Route.Type = "tcp"
config, _ := json.Marshal(r)
jsonConfig := json.RawMessage(config)
route := *r.Route
route.Config = &jsonConfig
return &route
}
type Event struct {
Event string
ID string
Error error
}