/
config.go
112 lines (102 loc) · 3.09 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
package proxy
import (
"github.com/bauerd/jqrp/jq"
"github.com/bauerd/jqrp/log"
"net"
"net/http"
"os"
"strconv"
"time"
)
// Config is the runtime configuration of jqrp.
type Config struct {
Port int
CacheSize int
EvaluationTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
DialTimeout time.Duration
DialKeepAlive time.Duration
TLSHandshakeTimeout time.Duration
ResponseHeaderTimeout time.Duration
ExpectContinueTimeout time.Duration
Level log.Level
}
// NewConfig returns a configuration read from environment variables.
func NewConfig() *Config {
return &Config{
Port: intFromEnvironment("PORT", 8989),
CacheSize: intFromEnvironment("CACHE_SIZE", 512),
EvaluationTimeout: durationFromEnvironment("EVAL_TIMEOUT", 0),
ReadTimeout: durationFromEnvironment("READ_TIMEOUT", 0),
WriteTimeout: durationFromEnvironment("WRITE_TIMEOUT", 0),
DialTimeout: durationFromEnvironment("DIAL_TIMEOUT", 0),
DialKeepAlive: durationFromEnvironment("DIAL_KEEPALIVE", 0),
TLSHandshakeTimeout: durationFromEnvironment("TLS_HANDSHAKE_TIMEOUT", 0),
ResponseHeaderTimeout: durationFromEnvironment("RESPONSE_HEADER_TIMEOUT", 0),
ExpectContinueTimeout: durationFromEnvironment("EXPECT_CONTINUE_TIMEOUT", 0),
}
}
func intFromEnvironment(key string, fallback int) int {
if value, ok := os.LookupEnv(key); ok {
i, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return fallback
}
return int(i)
}
return fallback
}
func durationFromEnvironment(key string, fallback int) time.Duration {
return time.Duration(intFromEnvironment(key, fallback)) * time.Millisecond
}
func levelFromEnvironment(key string, fallback log.Level) log.Level {
if value, ok := os.LookupEnv(key); ok {
switch value {
case "debug":
return log.Debug
case "info":
return log.Info
case "error":
return log.Error
}
}
return fallback
}
// Transport returns an HTTP transport with timeouts set.
func (c *Config) Transport() *http.Transport {
return &http.Transport{
Dial: (&net.Dialer{
Timeout: c.DialTimeout,
KeepAlive: c.DialKeepAlive,
}).Dial,
TLSHandshakeTimeout: c.TLSHandshakeTimeout,
ResponseHeaderTimeout: c.ResponseHeaderTimeout,
ExpectContinueTimeout: c.ExpectContinueTimeout,
}
}
// Evaluator returns a configured evaluator.
func (c *Config) Evaluator() (jq.Evaluator, error) {
compiler, err := c.compiler()
if err != nil {
return nil, err
}
if c.EvaluationTimeout <= 0 {
return jq.NewQueryEvaluator(compiler), nil
}
return jq.NewTimeoutEvaluator(jq.NewQueryEvaluator(compiler), c.EvaluationTimeout), nil
}
func (c *Config) compiler() (jq.Compiler, error) {
if c.CacheSize <= 0 {
return jq.QueryCompiler, nil
}
cachedCompiler, err := jq.NewCachedCompiler(jq.QueryCompiler, c.CacheSize)
if err != nil {
return nil, err
}
return cachedCompiler.Compiler, nil
}
// Logger returns a logger with level set.
func (c *Config) Logger() *log.Logger {
return log.New(levelFromEnvironment("LOG_LEVEL", log.Info))
}