This repository has been archived by the owner on Apr 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
api_config.go
256 lines (229 loc) · 9 KB
/
api_config.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package config
import (
"time"
)
// HTTPVerb defines the restful api http verb
type HTTPVerb string
const (
// MethodAny any method
MethodAny HTTPVerb = "ANY"
// MethodGet get
MethodGet HTTPVerb = "GET"
// MethodHead head
MethodHead HTTPVerb = "HEAD"
// MethodPost post
MethodPost HTTPVerb = "POST"
// MethodPut put
MethodPut HTTPVerb = "PUT"
// MethodPatch patch
MethodPatch HTTPVerb = "PATCH" // RFC 5789
// MethodDelete delete
MethodDelete HTTPVerb = "DELETE"
// MethodOptions options
MethodOptions HTTPVerb = "OPTIONS"
)
// RequestType describes the type of the request. could be DUBBO/HTTP and others that we might implement in the future
type RequestType string
const (
// DubboRequest represents the dubbo request
DubboRequest RequestType = "dubbo"
// HTTPRequest represents the http request
HTTPRequest RequestType = "http"
// GRPCRequest represents the grpc request
GRPCRequest RequestType = "grpc"
)
// APIConfig defines the data structure of the api gateway configuration
type APIConfig struct {
Name string `json:"name" yaml:"name"`
Description string `json:"description" yaml:"description"`
Resources []Resource `json:"resources" yaml:"resources"`
Definitions []Definition `json:"definitions" yaml:"definitions"`
}
// Resource defines the API path
type Resource struct {
ID int `json:"id,omitempty" yaml:"id,omitempty"`
Type string `json:"type" yaml:"type"` // Restful, Dubbo
Path string `json:"path" yaml:"path"`
Timeout time.Duration `json:"timeout" yaml:"timeout"`
Description string `json:"description" yaml:"description"`
Filters []Filter `json:"filters" yaml:"filters"`
Methods []Method `json:"methods" yaml:"methods"`
Resources []Resource `json:"resources,omitempty" yaml:"resources,omitempty"`
Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"`
}
const DefaultTimeoutStr = "1s"
// UnmarshalYAML Resource custom UnmarshalYAML
func (r *Resource) UnmarshalYAML(unmarshal func(interface{}) error) error {
s := &struct {
Timeout string `yaml:"timeout"`
}{}
type Alias Resource
alias := (*Alias)(r)
if err := unmarshal(alias); err != nil {
return err
}
if err := unmarshal(s); err != nil {
return err
}
// if timeout is empty must set a default value. if "" used to time.ParseDuration will err.
if s.Timeout == "" {
s.Timeout = DefaultTimeoutStr
}
d, err := time.ParseDuration(s.Timeout)
if err != nil {
return err
}
r.Timeout = d
return nil
}
// Filter filter with config
type Filter struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Config map[string]interface{} `json:"config,omitempty" yaml:"config,omitempty"`
}
// Method defines the method of the api
type Method struct {
ID int `json:"id,omitempty" yaml:"id,omitempty"`
ResourcePath string `json:"resourcePath" yaml:"resourcePath"`
Enable bool `json:"enable" yaml:"enable"` // true means the method is up and false means method is down
Timeout time.Duration `json:"timeout" yaml:"timeout"`
Mock bool `json:"mock" yaml:"mock"`
Filters []Filter `json:"filters" yaml:"filters"`
HTTPVerb `json:"httpVerb" yaml:"httpVerb"`
InboundRequest `json:"inboundRequest" yaml:"inboundRequest"`
IntegrationRequest `json:"integrationRequest" yaml:"integrationRequest"`
}
// UnmarshalYAML method custom UnmarshalYAML
func (m *Method) UnmarshalYAML(unmarshal func(interface{}) error) error {
type Alias Method
alias := (*Alias)(m)
if err := unmarshal(alias); err != nil {
return err
}
s := &struct {
Timeout string `yaml:"timeout"`
}{}
if err := unmarshal(s); err != nil {
return err
}
// if timeout is empty must set a default value. if "" used to time.ParseDuration will err.
if s.Timeout == "" {
s.Timeout = DefaultTimeoutStr
}
d, err := time.ParseDuration(s.Timeout)
if err != nil {
return err
}
m.Timeout = d
return nil
}
// InboundRequest defines the details of the inbound
type InboundRequest struct {
RequestType `json:"requestType" yaml:"requestType"` //http, TO-DO: dubbo
Headers []Params `json:"headers" yaml:"headers"`
QueryStrings []Params `json:"queryStrings" yaml:"queryStrings"`
RequestBody []BodyDefinition `json:"requestBody" yaml:"requestBody"`
}
// Params defines the simple parameter definition
type Params struct {
Name string `json:"name" yaml:"name"`
Type string `json:"type" yaml:"type"`
Required bool `json:"required" yaml:"required"`
}
// BodyDefinition connects the request body to the definitions
type BodyDefinition struct {
DefinitionName string `json:"definitionName" yaml:"definitionName"`
}
// IntegrationRequest defines the backend request format and target
type IntegrationRequest struct {
RequestType `json:"requestType" yaml:"requestType"` // dubbo, TO-DO: http
DubboBackendConfig `json:"dubboBackendConfig,inline,omitempty" yaml:"dubboBackendConfig,inline,omitempty"`
HTTPBackendConfig `json:"httpBackendConfig,inline,omitempty" yaml:"httpBackendConfig,inline,omitempty"`
MappingParams []MappingParam `json:"mappingParams,omitempty" yaml:"mappingParams,omitempty"`
}
// MappingParam defines the mapping rules of headers and queryStrings
type MappingParam struct {
Name string `json:"name,omitempty" yaml:"name"`
MapTo string `json:"mapTo,omitempty" yaml:"mapTo"`
MapType string `json:"mapType,omitempty" yaml:"mapType"`
}
// DubboBackendConfig defines the basic dubbo backend config
type DubboBackendConfig struct {
ClusterName string `yaml:"clusterName" json:"clusterName"`
ApplicationName string `yaml:"applicationName" json:"applicationName"`
Protocol string `yaml:"protocol" json:"protocol,omitempty" default:"dubbo"`
Group string `yaml:"group" json:"group"`
Version string `yaml:"version" json:"version"`
Interface string `yaml:"interface" json:"interface"`
Method string `yaml:"method" json:"method"`
Retries string `yaml:"retries" json:"retries,omitempty"`
}
// HTTPBackendConfig defines the basic dubbo backend config
type HTTPBackendConfig struct {
URL string `yaml:"url" json:"url,omitempty"`
// downstream host.
Host string `yaml:"host" json:"host,omitempty"`
// path to replace.
Path string `yaml:"path" json:"path,omitempty"`
// http protocol, http or https.
Schema string `yaml:"schema" json:"scheme,omitempty"`
}
// Definition defines the complex json request body
type Definition struct {
Name string `json:"name" yaml:"name"`
Schema string `json:"schema" yaml:"schema"` // use json schema
}
// Cluster defines the cluster config
type Cluster struct {
Name string `json:"name,omitempty" yaml:"name"` // cluster name
Type string `json:"type,omitempty" yaml:"type"` // cluster type
Address string `json:"address,omitempty" yaml:"address"` // cluster address
Port int `json:"port,omitempty" yaml:"port"` // cluster port
ID int `json:"id,omitempty" yaml:"id"` // cluster id
}
// RouteConfig defines the route config
type RouteConfig struct {
Routes []struct {
Match struct {
Prefix string `yaml:"prefix" json:"prefix"`
} `yaml:"match" json:"match"`
Route struct {
Cluster string `yaml:"cluster" json:"cluster"`
ClusterNotFoundResponseCode int `yaml:"cluster_not_found_response_code" json:"cluster_not_found_response_code"`
} `yaml:"route" json:"route"`
} `yaml:"routes" json:"routes"`
}
// HTTPFilters defines the http filter
type HTTPFilters []struct {
Name string `yaml:"name" json:"name"`
Config interface{} `yaml:"config" json:"config"`
}
// Listener defines the listener config
type Listener struct {
Name string `yaml:"name" json:"name"`
Address struct {
SocketAddress struct {
Address string `yaml:"address" json:"address"`
Port int `yaml:"port" json:"port"`
} `yaml:"socket-address" json:"socket_address"`
Name string `yaml:"name" json:"name"`
} `yaml:"address" json:"address"`
RouteConfig RouteConfig `yaml:"route_config" json:"route_config"`
HTTPFilters HTTPFilters `yaml:"http_filters" json:"http_filters"`
}