forked from simonmittag/j8a
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
173 lines (150 loc) · 4.34 KB
/
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
package jabba
import (
"encoding/json"
"github.com/ghodss/yaml"
"io/ioutil"
"os"
"regexp"
"sort"
"strings"
"time"
"github.com/rs/zerolog/log"
)
//Config is the system wide configuration for Jabba
type Config struct {
Policies map[string]Policy
Routes Routes
Resources map[string][]ResourceMapping
Connection Connection
}
const HTTP = "HTTP"
const TLS = "TLS"
var ConfigFile = "not specified"
func (config Config) read(file string) *Config {
jsonFile, err := os.Open(file)
defer jsonFile.Close()
if err != nil {
msg := "cannot find config file or unable to read server configuration, exiting..."
log.Fatal().Msg(msg)
panic(msg)
}
byteValue, _ := ioutil.ReadAll(jsonFile)
return config.parse(byteValue)
}
func (config Config) parse(jsonConfig []byte) *Config {
jsonConfig, _ = yaml.YAMLToJSON(jsonConfig)
json.Unmarshal(jsonConfig, &config)
//todo tell me about more of the config, number of routes
log.Debug().Msgf("parsed server configuration with %d live routes", len(config.Routes))
return &config
}
func (config Config) reApplyResourceNames() *Config {
for name := range config.Resources {
resourceMappings := config.Resources[name]
for i, resourceMapping := range resourceMappings {
resourceMapping.Name = name
resourceMappings[i] = resourceMapping
}
}
return &config
}
func (config Config) sortRoutes() *Config {
//prep routes with leading slash
for i, _ := range config.Routes {
if strings.Index(config.Routes[i].Path, "/") != 0 {
config.Routes[i].Path = "/" + config.Routes[i].Path
}
}
sort.Sort(Routes(config.Routes))
return &config
}
func (config Config) compileRoutePaths() *Config {
for i, route := range config.Routes {
config.Routes[i].Regex, _ = regexp.Compile("^" + route.Path)
}
return &config
}
func (config Config) reApplyResourceSchemes() *Config {
const http = "http"
const https = "https"
for name := range config.Resources {
resourceMappings := config.Resources[name]
for i, resourceMapping := range resourceMappings {
scheme := resourceMapping.URL.Scheme
if len(scheme) == 0 {
scheme = http
} else {
if strings.Contains(scheme, https) {
scheme = https
} else if strings.Contains(scheme, http) {
scheme = http
}
}
resourceMappings[i].URL.Scheme = scheme
}
}
return &config
}
func (config Config) addDefaultPolicy() *Config {
defaultPolicy := new(Policy)
lw := LabelWeight{
Label: "default",
Weight: 1.0,
}
var labelWeights []LabelWeight
labelWeights = append(labelWeights, lw)
*defaultPolicy = labelWeights
if config.Policies == nil {
config.Policies = make(map[string]Policy)
}
config.Policies["default"] = *defaultPolicy
return &config
}
func (config Config) setDefaultDownstreamParams() *Config {
if config.Connection.Downstream.ReadTimeoutSeconds == 0 {
config.Connection.Downstream.ReadTimeoutSeconds = 120
}
if config.Connection.Downstream.RoundTripTimeoutSeconds == 0 {
config.Connection.Downstream.RoundTripTimeoutSeconds = 240
}
if config.Connection.Downstream.IdleTimeoutSeconds == 0 {
config.Connection.Downstream.IdleTimeoutSeconds = 120
}
if len(config.Connection.Downstream.Mode) == 0 {
config.Connection.Downstream.Mode = HTTP
} else {
config.Connection.Downstream.Mode = strings.ToUpper(config.Connection.Downstream.Mode)
}
if config.Connection.Downstream.Port == 0 {
if config.isTLSMode() {
config.Connection.Downstream.Port = 443
} else {
config.Connection.Downstream.Port = 8080
}
}
return &config
}
func (config Config) isTLSMode() bool {
return strings.ToUpper(config.Connection.Downstream.Mode) == TLS
}
func (config Config) setDefaultUpstreamParams() *Config {
if config.Connection.Upstream.SocketTimeoutSeconds == 0 {
config.Connection.Upstream.SocketTimeoutSeconds = 3
}
if config.Connection.Upstream.ReadTimeoutSeconds == 0 {
config.Connection.Upstream.ReadTimeoutSeconds = 120
}
if config.Connection.Upstream.IdleTimeoutSeconds == 0 {
config.Connection.Upstream.IdleTimeoutSeconds = 120
}
if config.Connection.Upstream.PoolSize == 0 {
config.Connection.Upstream.PoolSize = 32768
}
if config.Connection.Upstream.MaxAttempts == 0 {
config.Connection.Upstream.MaxAttempts = 1
}
return &config
}
func (config Config) getDownstreamRoundTripTimeoutDuration() time.Duration {
return time.Duration(time.Second * time.Duration(config.Connection.Downstream.RoundTripTimeoutSeconds))
}