-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.go
201 lines (167 loc) · 5.37 KB
/
route.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package api
import (
"context"
"strings"
"time"
"github.com/asaskevich/govalidator"
"github.com/moonrhythm/validator"
)
var routeTargetPrefix = []string{
"deployment://",
"redirect://",
"ipfs://",
"ipns://",
"dnslink://",
}
func RouteTargetPrefix() []string {
xs := make([]string, len(routeTargetPrefix))
copy(xs, routeTargetPrefix)
return xs
}
type Route interface {
Create(ctx context.Context, m *RouteCreate) (*Empty, error)
CreateV2(ctx context.Context, m *RouteCreateV2) (*Empty, error)
Get(ctx context.Context, m *RouteGet) (*RouteItem, error)
List(ctx context.Context, m *RouteList) (*RouteListResult, error)
Delete(ctx context.Context, m *RouteDelete) (*Empty, error)
}
type RouteCreate struct {
Project string `json:"project" yaml:"project"`
Location string `json:"location" yaml:"location"`
Domain string `json:"domain" yaml:"domain"`
Path string `json:"path" yaml:"path"`
Deployment string `json:"deployment" yaml:"deployment"`
}
func (m *RouteCreate) Valid() error {
v := validator.New()
v.Must(m.Project != "", "project required")
v.Must(m.Location != "", "location required")
v.Must(govalidator.IsDNSName(m.Domain), "domain invalid")
if m.Path != "" {
v.Must(strings.HasPrefix(m.Path, "/"), "path must start with /")
}
return WrapValidate(v)
}
type RouteConfig struct {
BasicAuth *RouteConfigBasicAuth `json:"basicAuth" yaml:"basicAuth"`
ForwardAuth *RouteConfigForwardAuth `json:"forwardAuth" yaml:"forwardAuth"`
}
type RouteConfigBasicAuth struct {
User string `json:"user" yaml:"user"`
Password string `json:"password" yaml:"password"`
}
type RouteConfigForwardAuth struct {
Target string `json:"target" yaml:"target"`
AuthRequestHeaders []string `json:"authRequestHeaders" yaml:"authRequestHeaders"`
AuthResponseHeaders []string `json:"authResponseHeaders" yaml:"authResponseHeaders"`
}
type RouteCreateV2 struct {
Project string `json:"project" yaml:"project"`
Location string `json:"location" yaml:"location"`
Domain string `json:"domain" yaml:"domain"`
Path string `json:"path" yaml:"path"`
Target string `json:"target" yaml:"target"`
Config RouteConfig `json:"config" yaml:"config"`
}
func (m *RouteCreateV2) Valid() error {
v := validator.New()
v.Must(m.Project != "", "project required")
v.Must(m.Location != "", "location required")
v.Must(govalidator.IsDNSName(m.Domain), "domain invalid")
if m.Path != "" {
v.Must(strings.HasPrefix(m.Path, "/"), "path must start with /")
}
v.Must(validRouteTarget(m.Target), "target invalid")
if m.Config.BasicAuth != nil {
v.Must(m.Config.ForwardAuth == nil, "basicAuth and forwardAuth cannot be used together")
v.Must(m.Config.BasicAuth.User != "", "user required")
v.Must(m.Config.BasicAuth.Password != "", "password required")
}
if m.Config.ForwardAuth != nil {
if v.Must(m.Config.ForwardAuth.Target != "", "target required") {
v.Must(validURL(m.Config.ForwardAuth.Target), "target invalid")
v.Must(strings.HasPrefix(m.Target, "http://"), "target must start with http://")
}
}
return WrapValidate(v)
}
type RouteGet struct {
Project string `json:"project" yaml:"project"`
Location string `json:"location" yaml:"location"`
Domain string `json:"domain" yaml:"domain"`
Path string `json:"path" yaml:"path"`
}
func (m *RouteGet) Valid() error {
v := validator.New()
v.Must(m.Project != "", "project required")
v.Must(m.Location != "", "location required")
v.Must(govalidator.IsDNSName(m.Domain), "domain invalid")
if m.Path != "" {
v.Must(strings.HasPrefix(m.Path, "/"), "path must start with /")
}
return WrapValidate(v)
}
type RouteList struct {
Project string `json:"project" yaml:"project"`
Location string `json:"location" yaml:"location"`
}
func (m *RouteList) Valid() error {
v := validator.New()
v.Must(m.Project != "", "project required")
return WrapValidate(v)
}
type RouteListResult struct {
Items []*RouteItem `json:"items" yaml:"items"`
}
func (m *RouteListResult) Table() [][]string {
table := [][]string{
{"DOMAIN", "PATH", "DEPLOYMENT", "LOCATION"},
}
for _, x := range m.Items {
table = append(table, []string{
x.Domain,
x.Path,
x.Deployment,
x.Location,
})
}
return table
}
type RouteItem struct {
Location string `json:"location" yaml:"location"`
Domain string `json:"domain" yaml:"domain"`
Path string `json:"path" yaml:"path"`
Target string `json:"target" yaml:"target"`
Deployment string `json:"deployment" yaml:"deployment"`
Config RouteConfig `json:"config" yaml:"config"`
CreatedAt time.Time `json:"createdAt" yaml:"createdAt"`
CreatedBy string `json:"createdBy" yaml:"createdBy"`
}
func (m *RouteItem) Table() [][]string {
table := [][]string{
{"DOMAIN", "PATH", "TARGET", "LOCATION"},
{
m.Domain,
m.Path,
m.Target,
m.Location,
},
}
return table
}
type RouteDelete struct {
Project string `json:"project" yaml:"project"`
Location string `json:"location" yaml:"location"`
Domain string `json:"domain" yaml:"domain"`
Path string `json:"path" yaml:"path"`
}
func (m *RouteDelete) Valid() error {
v := validator.New()
v.Must(m.Project != "", "project required")
v.Must(m.Location != "", "location required")
v.Must(govalidator.IsDNSName(m.Domain), "domain invalid")
if m.Path != "" {
v.Must(strings.HasPrefix(m.Path, "/"), "path must start with /")
}
return WrapValidate(v)
}