-
Notifications
You must be signed in to change notification settings - Fork 14
/
readconfig.go
170 lines (127 loc) · 4.6 KB
/
readconfig.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
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package main
import (
"strconv"
"time"
)
// HasEnv provides interface for os.Getenv
type HasEnv interface {
Getenv(key string) string
}
// ReadConfig constitutes config from env variables
type ReadConfig struct {
}
func isBoolValueSet(val string) bool {
return len(val) > 0
}
func parseBoolValue(val string) bool {
if val == "true" {
return true
}
return false
}
func parseIntOrDurationValue(val string, fallback time.Duration) time.Duration {
if len(val) > 0 {
parsedVal, parseErr := strconv.Atoi(val)
if parseErr == nil && parsedVal >= 0 {
return time.Duration(parsedVal) * time.Second
}
}
duration, durationErr := time.ParseDuration(val)
if durationErr != nil {
return fallback
}
return duration
}
func parseIntValue(val string, fallback int) int {
if len(val) > 0 {
parsedVal, parseErr := strconv.Atoi(val)
if parseErr == nil && parsedVal >= 0 {
return parsedVal
}
}
return fallback
}
// Read fetches config from environmental variables.
func (ReadConfig) Read(hasEnv HasEnv) WatchdogConfig {
cfg := WatchdogConfig{
writeDebug: false,
cgiHeaders: true,
combineOutput: true,
}
defaultTimeout := time.Second * 30
cfg.faasProcess = hasEnv.Getenv("fprocess")
cfg.readTimeout = parseIntOrDurationValue(hasEnv.Getenv("read_timeout"), defaultTimeout)
cfg.writeTimeout = parseIntOrDurationValue(hasEnv.Getenv("write_timeout"), defaultTimeout)
cfg.healthcheckInterval = parseIntOrDurationValue(hasEnv.Getenv("healthcheck_interval"), cfg.writeTimeout)
// time.Second * 0 means that there is no hard i.e. "exec" timeout set
cfg.execTimeout = parseIntOrDurationValue(hasEnv.Getenv("exec_timeout"), time.Second*0)
cfg.port = parseIntValue(hasEnv.Getenv("port"), 8080)
writeDebugEnv := hasEnv.Getenv("write_debug")
if isBoolValueSet(writeDebugEnv) {
cfg.writeDebug = parseBoolValue(writeDebugEnv)
}
cgiHeadersEnv := hasEnv.Getenv("cgi_headers")
if isBoolValueSet(cgiHeadersEnv) {
cfg.cgiHeaders = parseBoolValue(cgiHeadersEnv)
}
cfg.marshalRequest = parseBoolValue(hasEnv.Getenv("marshal_request"))
cfg.debugHeaders = parseBoolValue(hasEnv.Getenv("debug_headers"))
cfg.suppressLock = parseBoolValue(hasEnv.Getenv("suppress_lock"))
cfg.contentType = hasEnv.Getenv("content_type")
if isBoolValueSet(hasEnv.Getenv("combine_output")) {
cfg.combineOutput = parseBoolValue(hasEnv.Getenv("combine_output"))
}
cfg.jwtAuthentication = parseBoolValue(hasEnv.Getenv("jwt_auth"))
cfg.jwtAuthDebug = parseBoolValue(hasEnv.Getenv("jwt_auth_debug"))
cfg.jwtAuthLocal = parseBoolValue(hasEnv.Getenv("jwt_auth_local"))
cfg.metricsPort = 8081
cfg.maxInflight = parseIntValue(hasEnv.Getenv("max_inflight"), 0)
return cfg
}
// WatchdogConfig for the process.
type WatchdogConfig struct {
// HTTP read timeout
readTimeout time.Duration
// HTTP write timeout
writeTimeout time.Duration
// healthcheckInterval is the interval that an external service runs its health checks to
// detect health and remove the watchdog from its pool of endpoints
healthcheckInterval time.Duration
// faasProcess is the process to exec
faasProcess string
// duration until faasProcess is killed, set to time.Second * 0 to disable
execTimeout time.Duration
// writeDebug write console stdout statements to the container
writeDebug bool
// marshal header and body via JSON
marshalRequest bool
// cgiHeaders will make environmental variables available with all the HTTP headers.
cgiHeaders bool
// prints out all incoming and out-going HTTP headers
debugHeaders bool
// Don't write a lock file to /tmp/
suppressLock bool
// contentType forces a specific pre-defined value for all responses
contentType string
// port for HTTP server
port int
// combineOutput combines stderr and stdout in response
combineOutput bool
// metricsPort is the HTTP port to serve metrics on
metricsPort int
// jwtAuthentication enables JWT authentication for the watchdog
// using the OpenFaaS gateway as the issuer.
jwtAuthentication bool
// jwtAuthDebug enables debug logging for the JWT authentication middleware.
jwtAuthDebug bool
// jwtAuthLocal indicates wether the JWT authentication middleware should use a port-forwarded or
// local gateway running at `http://127.0.0.1:8000` instead of attempting to reach it via an in-cluster service
jwtAuthLocal bool
// maxInflight limits the number of simultaneous
// requests that the watchdog allows concurrently.
// Any request which exceeds this limit will
// have an immediate response of 429.
maxInflight int
}