-
Notifications
You must be signed in to change notification settings - Fork 177
/
loader.go
162 lines (139 loc) · 4.58 KB
/
loader.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
/*
* Copyright 2018 Comcast Cable Communications Management, LLC
*
* Licensed 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 (
"errors"
"fmt"
"net/url"
"strconv"
"strings"
"time"
)
// Load returns the Application Configuration, starting with a default config,
// then overriding with any provided config file, then env vars, and finally flags
func Load(applicationName string, applicationVersion string, arguments []string) (*Config, *Flags, error) {
c := NewConfig()
flags, err := parseFlags(applicationName, arguments) // Parse here to get config file path and version flags
if err != nil {
return nil, flags, err
}
if flags.PrintVersion {
return nil, flags, nil
}
if err := c.loadFile(flags); err != nil && flags.customPath {
// a user-provided path couldn't be loaded. return the error for the application to handle
return nil, flags, err
}
c.loadEnvVars()
c.loadFlags(flags) // load parsed flags to override file and envs
// set the default origin url from the flags
if d, ok := c.Origins["default"]; ok {
if c.providedOriginURL != "" {
url, err := url.Parse(c.providedOriginURL)
if err != nil {
return nil, flags, err
}
if c.providedOriginType != "" {
d.OriginType = c.providedOriginType
}
d.OriginURL = c.providedOriginURL
d.Scheme = url.Scheme
d.Host = url.Host
d.PathPrefix = url.Path
}
// If the user has configured their own origins, and one of them is not "default"
// then Trickster will not use the auto-created default origin
if d.OriginURL == "" {
delete(c.Origins, "default")
}
if c.providedOriginType != "" {
d.OriginType = c.providedOriginType
}
}
if len(c.Origins) == 0 {
return nil, flags, errors.New("no valid origins configured")
}
for k, n := range c.NegativeCacheConfigs {
for c := range n {
ci, err := strconv.Atoi(c)
if err != nil {
return nil, flags, fmt.Errorf(`invalid negative cache config in %s: %s is not a valid status code`, k, c)
}
if ci < 400 || ci >= 600 {
return nil, flags, fmt.Errorf(`invalid negative cache config in %s: %s is not a valid status code`, k, c)
}
}
}
for k, o := range c.Origins {
if o.OriginType == "" {
return nil, flags, fmt.Errorf(`missing origin-type for origin "%s"`, k)
}
if o.OriginType != "rule" && o.OriginURL == "" {
return nil, flags, fmt.Errorf(`missing origin-url for origin "%s"`, k)
}
url, err := url.Parse(o.OriginURL)
if err != nil {
return nil, flags, err
}
if strings.HasSuffix(url.Path, "/") {
url.Path = url.Path[0 : len(url.Path)-1]
}
o.Name = k
o.Scheme = url.Scheme
o.Host = url.Host
o.PathPrefix = url.Path
o.Timeout = time.Duration(o.TimeoutSecs) * time.Second
o.BackfillTolerance = time.Duration(o.BackfillToleranceSecs) * time.Second
o.TimeseriesRetention = time.Duration(o.TimeseriesRetentionFactor)
o.TimeseriesTTL = time.Duration(o.TimeseriesTTLSecs) * time.Second
o.FastForwardTTL = time.Duration(o.FastForwardTTLSecs) * time.Second
o.MaxTTL = time.Duration(o.MaxTTLSecs) * time.Second
if o.CompressableTypeList != nil {
o.CompressableTypes = make(map[string]bool)
for _, v := range o.CompressableTypeList {
o.CompressableTypes[v] = true
}
}
if o.CacheKeyPrefix == "" {
o.CacheKeyPrefix = o.Host
}
nc, ok := c.NegativeCacheConfigs[o.NegativeCacheName]
if !ok {
return nil, flags, fmt.Errorf(`invalid negative cache name: %s`, o.NegativeCacheName)
}
nc2 := map[int]time.Duration{}
for c, s := range nc {
ci, _ := strconv.Atoi(c)
nc2[ci] = time.Duration(s) * time.Second
}
o.NegativeCache = nc2
// enforce MaxTTL
if o.TimeseriesTTLSecs > o.MaxTTLSecs {
o.TimeseriesTTLSecs = o.MaxTTLSecs
o.TimeseriesTTL = o.MaxTTL
}
// unlikely but why not spend a few nanoseconds to check it at startup
if o.FastForwardTTLSecs > o.MaxTTLSecs {
o.FastForwardTTLSecs = o.MaxTTLSecs
o.FastForwardTTL = o.MaxTTL
}
}
for _, c := range c.Caches {
c.Index.FlushInterval = time.Duration(c.Index.FlushIntervalSecs) * time.Second
c.Index.ReapInterval = time.Duration(c.Index.ReapIntervalSecs) * time.Second
}
return c, flags, nil
}