-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
server_options.go
116 lines (100 loc) · 2.48 KB
/
server_options.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
package vulcain
import (
"fmt"
"net/url"
"os"
"strconv"
"strings"
"time"
)
// ServerOptions stores the server's options
//
// Deprecated: use the Caddy server module or the standalone library instead
type ServerOptions struct {
Debug bool
Addr string
Upstream *url.URL
EarlyHints bool
MaxPushes int
AcmeHosts []string
AcmeCertDir string
CertFile string
KeyFile string
ReadTimeout time.Duration
WriteTimeout time.Duration
Compress bool
OpenAPIFile string
}
// NewOptionsFromEnv creates a new option instance from environment
// It returns an error if mandatory env env vars are missing
//
// Deprecated: use the Caddy server module or the standalone library instead
func NewOptionsFromEnv() (*ServerOptions, error) {
var err error
readTimeout, err := parseDurationFromEnvVar("READ_TIMEOUT", time.Duration(0))
if err != nil {
return nil, err
}
writeTimeout, err := parseDurationFromEnvVar("WRITE_TIMEOUT", time.Duration(0))
if err != nil {
return nil, err
}
upstream, err := url.Parse(os.Getenv("UPSTREAM"))
if err != nil {
return nil, err
}
var maxPushes int
maxPushesStr := os.Getenv("MAX_PUSHES")
if maxPushesStr == "" {
maxPushes = -1
} else {
maxPushes, err = strconv.Atoi(maxPushesStr)
if err != nil {
return nil, fmt.Errorf(`MAX_PUSHES: invalid value "%s" (%s)`, maxPushesStr, err)
}
}
earlyHints := os.Getenv("EARLY_HINTS")
o := &ServerOptions{
os.Getenv("DEBUG") == "1",
os.Getenv("ADDR"),
upstream,
earlyHints != "" && earlyHints != "0",
maxPushes,
splitVar(os.Getenv("ACME_HOSTS")),
os.Getenv("ACME_CERT_DIR"),
os.Getenv("CERT_FILE"),
os.Getenv("KEY_FILE"),
readTimeout,
writeTimeout,
os.Getenv("COMPRESS") != "0",
os.Getenv("OPENAPI_FILE"),
}
missingEnv := make([]string, 0, 2)
if len(o.CertFile) != 0 && len(o.KeyFile) == 0 {
missingEnv = append(missingEnv, "KEY_FILE")
}
if len(o.KeyFile) != 0 && len(o.CertFile) == 0 {
missingEnv = append(missingEnv, "CERT_FILE")
}
if len(missingEnv) > 0 {
return nil, fmt.Errorf("The following environment variable must be defined: %s", missingEnv)
}
return o, nil
}
func splitVar(v string) []string {
if v == "" {
return []string{}
}
return strings.Split(v, ",")
}
func parseDurationFromEnvVar(k string, d time.Duration) (time.Duration, error) {
v := os.Getenv(k)
if v == "" {
return d, nil
}
dur, err := time.ParseDuration(v)
if err == nil {
return dur, nil
}
return time.Duration(0), fmt.Errorf("%s: %s", k, err)
}