-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·169 lines (147 loc) · 3.87 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"path"
"strings"
)
func loadConfig(configFile string) (config ConfigModel, err error) {
file, err := ioutil.ReadFile(configFile)
if err != nil {
return config, err
}
err = json.Unmarshal([]byte(file), &config)
if err != nil {
return config, err
}
return config, nil
}
func inStringSlice(item string, slice []string) bool {
for _, v := range slice {
if v == item {
return true
}
}
return false
}
func getFilePath(baseDir string, filePrefix string, separator string, serviceName string) string {
prefix := ""
if filePrefix != "" {
prefix = fmt.Sprintf("%s%s", filePrefix, separator)
}
fileName := fmt.Sprintf("%s%s.json", prefix, serviceName)
fPath := path.Join(path.Dir(baseDir), fileName)
return fPath
}
func doRequest(url string, response interface{}) (err error) {
client := http.Client{}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}
res, err := client.Do(req)
if err != nil {
return err
}
if res.Body != nil {
defer res.Body.Close()
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
_ = json.Unmarshal(body, &response)
return err
}
func transformSwaggerResponse(serviceConfig Service, response SwaggerResponse) (paths []KrakendPath) {
var headerToPass []string
for _, v := range response.SecurityDefinitions {
if v.In == HttpAdditionalInformation {
headerToPass = append(headerToPass, v.Name)
}
}
for url, swaggerPaths := range response.Paths {
if inStringSlice(url, serviceConfig.ExcludedEndpoints) {
continue
}
for method, swaggerMethod := range swaggerPaths {
var qParams []string
for _, param := range swaggerMethod.Parameters {
if param.In == SwaggerQueryStringKey {
qParams = append(qParams, param.Name)
}
}
kPath := KrakendPath{
Endpoint: url,
OutputEncoding: serviceConfig.KrakendOutputEncoding,
QuerystringParams: qParams,
Method: strings.ToUpper(method),
HeadersToPass: headerToPass,
Backend: []KrakendBackend{
{
Encoding: serviceConfig.KrakendBackendEncoding,
URLPattern: url,
Host: []string{serviceConfig.InternalAddress},
},
},
}
paths = append(paths, kPath)
}
}
return paths
}
func crossMapping(serviceConfig Service) (paths []KrakendPath) {
for _, router := range serviceConfig.CrossMapping {
from := strings.ReplaceAll(router.From, ServiceReplacementKey, serviceConfig.Name)
to := strings.ReplaceAll(router.To, ServiceReplacementKey, serviceConfig.Name)
kPath := KrakendPath{
Endpoint: from,
OutputEncoding: serviceConfig.KrakendOutputEncoding,
Method: router.Method,
HeadersToPass: router.HeadersToPass,
Backend: []KrakendBackend{
{
Encoding: serviceConfig.KrakendBackendEncoding,
URLPattern: to,
Host: []string{router.InternalAddress},
},
},
}
paths = append(paths, kPath)
}
return paths
}
func main() {
config, err := loadConfig("config.json")
if err != nil {
log.Fatal(err)
return
}
for _, service := range config.Services {
var paths []KrakendPath
response := SwaggerResponse{}
err := doRequest(service.Url, &response)
if err != nil {
log.Println(service.Name, err.Error())
continue
}
swaggerPaths := transformSwaggerResponse(service, response)
paths = append(paths, swaggerPaths...)
crossPaths := crossMapping(service)
paths = append(paths, crossPaths...)
jsonResult, err := json.Marshal(paths)
if err != nil {
log.Fatal("json file creation error ", err)
}
endpointScope := string(jsonResult)[1:]
endpointScope = endpointScope[:len(endpointScope)-1]
filePath := getFilePath(config.BaseDir, config.FilePrefix, config.PrefixSeparator, service.Name)
err = ioutil.WriteFile(filePath, []byte(endpointScope), 0644)
if err != nil {
log.Panic(err)
}
}
}