-
Notifications
You must be signed in to change notification settings - Fork 0
/
readconfig.go
152 lines (114 loc) · 3.67 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
// 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,
}
cfg.faasProcess = hasEnv.Getenv("fprocess")
cfg.functionName = hasEnv.Getenv("function_name")
cfg.readTimeout = parseIntOrDurationValue(hasEnv.Getenv("read_timeout"), time.Second*5)
cfg.writeTimeout = parseIntOrDurationValue(hasEnv.Getenv("write_timeout"), time.Second*5)
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.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
// faasProcess is the process to exec
faasProcess string
// duration until the faasProcess will be killed
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
// 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
// functionName sets name of the function for logging
// cpu and memory usage
functionName string
}