-
Notifications
You must be signed in to change notification settings - Fork 10
/
opts.go
131 lines (111 loc) · 3.04 KB
/
opts.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
package postman
import (
"encoding/json"
"errors"
"fmt"
"net"
"strconv"
"github.com/Masterminds/semver/v3"
"github.com/luraproject/lura/v2/config"
)
var (
errInvalidServiceConfig = errors.New("invalid service config")
errMissingEndpointConfig = errors.New("missing endpoint config")
errMissingVersion = errors.New("missing version config")
errInvalidSemver = errors.New("the provided version is not in semver format")
)
func parseServiceOptions(serviceConfig *config.ServiceConfig) (*serviceOptions, error) {
opts := &serviceOptions{
Name: serviceConfig.Name,
Description: defaultDescription,
}
raw, ok := serviceConfig.ExtraConfig[namespace].(map[string]interface{})
if !ok {
// Backwards compatibility: there's no specific service config, we should proceed without problems
return opts, nil
}
tmp, err := json.Marshal(raw)
if err != nil {
return nil, errInvalidServiceConfig
}
if err := json.Unmarshal(tmp, opts); err != nil {
return nil, errInvalidServiceConfig
}
return opts, nil
}
func findFolderOptions(serviceOpts *serviceOptions, name string) *folderOptions {
for _, f := range serviceOpts.Folder {
if f.Name == name {
return &f
}
}
return nil
}
func parseEndpointOptions(endpointConfig *config.EndpointConfig) (*endpointOptions, error) {
opts := &endpointOptions{}
endpointCfg, ok := endpointConfig.ExtraConfig[namespace].(map[string]interface{})
if !ok {
return nil, errMissingEndpointConfig
}
tmp, err := json.Marshal(endpointCfg)
if err != nil {
return nil, fmt.Errorf("invalid endpoint config: %s %s", endpointConfig.Method, endpointConfig.Endpoint)
}
if err := json.Unmarshal(tmp, opts); err != nil {
return nil, fmt.Errorf("invalid endpoint config: %s %s", endpointConfig.Method, endpointConfig.Endpoint)
}
return opts, nil
}
func parseVersion(serviceOpts *serviceOptions) (*Version, error) {
if serviceOpts.Version == "" {
return nil, errMissingVersion
}
v, err := semver.NewVersion(serviceOpts.Version)
if err != nil {
return nil, errInvalidSemver
}
version := &Version{
Major: v.Major(),
Minor: v.Minor(),
Patch: v.Patch(),
}
return version, nil
}
func parseVariables(cfg *config.ServiceConfig) []Variable {
address := "localhost"
if cfg.Address != "" {
address = cfg.Address
}
schema := "http"
if cfg.TLS != nil && !cfg.TLS.IsDisabled {
schema = "https"
}
return []Variable{
{
ID: hash("HOST"),
Key: "HOST",
Type: "string",
Value: net.JoinHostPort(address, strconv.Itoa(cfg.Port)),
},
{
ID: hash("SCHEMA"),
Key: "SCHEMA",
Type: "string",
Value: schema,
},
}
}
type serviceOptions struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Version string `json:"version,omitempty"`
Folder []folderOptions `json:"folder,omitempty"`
}
type folderOptions struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
}
type endpointOptions struct {
folderOptions
Folder string `json:"folder,omitempty"`
}