-
Notifications
You must be signed in to change notification settings - Fork 8
/
conf.go
218 lines (178 loc) · 6.19 KB
/
conf.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
package conf
import (
"github.com/caarlos0/env/v6"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"time"
)
const configFileName = "restql.yml"
type requestIDConf struct {
Header string `yaml:"header"`
Strategy string `yaml:"strategy"`
}
type timeoutConf struct {
Duration string `yaml:"duration"`
}
type corsConf struct {
AllowOrigin string `yaml:"allowOrigin" env:"RESTQL_CORS_ALLOW_ORIGIN"`
AllowMethods string `yaml:"allowMethods" env:"RESTQL_CORS_ALLOW_METHODS"`
AllowHeaders string `yaml:"allowHeaders" env:"RESTQL_CORS_ALLOW_HEADERS"`
ExposeHeaders string `yaml:"exposeHeaders" env:"RESTQL_CORS_EXPOSE_HEADERS"`
MaxAge int `yaml:"maxAge" env:"RESTQL_CORS_MAX_AGE"`
AllowCredentials bool `yaml:"allowCredentials" env:"RESTQL_CORS_ALLOW_CREDENTIALS"`
}
type requestCancellationConf struct {
Enabled bool `yaml:"enabled"`
WatchInterval time.Duration `yaml:"watchInterval"`
}
// Config represents all parameters allowed in restQL runtime.
type Config struct {
HTTP struct {
ForwardPrefix string `yaml:"forwardPrefix" env:"RESTQL_FORWARD_PREFIX"`
GlobalQueryTimeout time.Duration `env:"RESTQL_QUERY_GLOBAL_TIMEOUT" envDefault:"30s"`
QueryResourceTimeout time.Duration `env:"RESTQL_QUERY_RESOURCE_TIMEOUT" envDefault:"5s"`
Server struct {
APIAddr string `env:"RESTQL_PORT,required"`
APIHealthAddr string `env:"RESTQL_HEALTH_PORT,required"`
PropfAddr string `env:"RESTQL_PPROF_PORT"`
EnablePprof bool `env:"RESTQL_ENABLE_PPROF"`
EnableFullPprof bool `env:"RESTQL_ENABLE_FULL_PPROF"`
Admin struct {
Enable bool `yaml:"enable" env:"RESTQL_ADMIN_ENABLE"`
AuthorizationCode string `yaml:"authorizationCode" env:"RESTQL_ADMIN_AUTHORIZATION_CODE"`
} `yaml:"admin"`
GracefulShutdownTimeout time.Duration `yaml:"gracefulShutdownTimeout"`
ReadTimeout time.Duration `yaml:"readTimeout"`
IdleTimeout time.Duration `yaml:"idleTimeout"`
Middlewares struct {
RequestID *requestIDConf `yaml:"requestId"`
Timeout *timeoutConf `yaml:"timeout"`
Cors *corsConf `yaml:"cors"`
RequestCancellation *requestCancellationConf `yaml:"requestCancellation"`
} `yaml:"middlewares"`
} `yaml:"server"`
Client struct {
DnsRefreshInterval time.Duration `yaml:"dnsRefreshInterval"`
ConnTimeout time.Duration `yaml:"connectionTimeout"`
MaxRequestTimeout time.Duration `yaml:"maxRequestTimeout"`
MaxConnsPerHost int `yaml:"maxConnectionsPerHost"`
MaxIdleConns int `yaml:"maxIdleConnections"`
MaxIdleConnsPerHost int `yaml:"maxIdleConnectionsPerHost"`
MaxIdleConnDuration time.Duration `yaml:"maxIdleConnectionDuration"`
} `yaml:"client"`
} `yaml:"http"`
Logging struct {
Enable bool `yaml:"enable" env:"RESTQL_LOGGING_ENABLE"`
TimestampFieldName string `yaml:"timestampFieldName"`
TimestampFieldFormat string `yaml:"timestampFieldFormat"`
Level string `yaml:"level" env:"RESTQL_LOGGING_LEVEL"`
Format string `yaml:"format"`
} `yaml:"logging"`
Cache struct {
Mappings struct {
MaxSize int `yaml:"maxSize" env:"RESTQL_CACHE_MAPPINGS_MAX_SIZE"`
Expiration time.Duration `yaml:"expiration" env:"RESTQL_CACHE_MAPPINGS_EXPIRATION"`
RefreshInterval time.Duration `yaml:"refreshInterval" env:"RESTQL_CACHE_MAPPINGS_REFRESH_INTERVAL"`
RefreshQueueLength int `yaml:"refreshQueueLength" env:"RESTQL_CACHE_MAPPINGS_REFRESH_QUEUE_LENGTH"`
} `yaml:"mappings"`
Query struct {
MaxSize int `yaml:"maxSize" env:"RESTQL_CACHE_QUERY_MAX_SIZE"`
} `yaml:"query"`
Parser struct {
MaxSize int `yaml:"maxSize" env:"RESTQL_CACHE_PARSER_MAX_SIZE"`
} `yaml:"parser"`
} `yaml:"cache"`
Plugins struct {
DisableDatabase bool `yaml:"disableDatabase" env:"RESTQL_PLUGINS_DATABASE_DISABLE"`
} `yaml:"plugins"`
Tenant string `env:"RESTQL_TENANT"`
Mappings map[string]string `yaml:"mappings"`
TenantMappings map[string]map[string]string `yaml:"tenants"`
Queries map[string]map[string][]string `yaml:"queries"`
Env EnvSource
Build string
}
// Load returns a Config build from the
// defaults, YAML configuration file and
// environment variables.
func Load(build string) (*Config, error) {
cfg := Config{}
readDefaults(&cfg)
err := yaml.Unmarshal(readConfigFile(), &cfg)
if err != nil {
return nil, err
}
err = env.Parse(&cfg)
if err != nil {
return nil, err
}
cfg.Build = build
cfg.Env = EnvSource{}
return &cfg, nil
}
func readConfigFile() []byte {
path := getConfigFilepath()
if path == "" {
log.Printf("[WARN] no config file present")
return nil
}
data, err := ioutil.ReadFile(path)
if err != nil {
log.Printf("[ERROR] could not load file at %s", path)
return nil
}
return data
}
func getConfigFilepath() string {
envConfigFilepath := os.Getenv("RESTQL_CONFIG")
if envConfigFilepath != "" {
fileAtCustom, err := filepath.Abs(envConfigFilepath)
if err != nil {
log.Printf("[DEBUG] failed to find directory: %v", err)
}
return fileAtCustom
}
fileAtRoot := filepath.Join(".", configFileName)
if doesFileExist(fileAtRoot) {
return fileAtRoot
}
homeDir, err := os.UserHomeDir()
if err != nil {
log.Printf("[DEBUG] failed to find home directory: %v", err)
return ""
}
fileAtHome := filepath.Join(homeDir, configFileName)
if doesFileExist(fileAtHome) {
return fileAtHome
}
return ""
}
func doesFileExist(filepath string) bool {
info, err := os.Stat(filepath)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
// EnvSource allows access to environment variables.
type EnvSource struct{}
// GetString returns the environment variable value
// for the given key.
func (e EnvSource) GetString(key string) string {
return os.Getenv("RESTQL_" + key)
}
// GetAll return all the environment variables.
func (e EnvSource) GetAll() map[string]string {
result := make(map[string]string)
for _, envVar := range os.Environ() {
pair := strings.SplitN(envVar, "=", 2)
if strings.HasPrefix(pair[0], "RESTQL_") {
result[pair[0]] = pair[1]
}
}
return result
}