-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
81 lines (68 loc) · 3.31 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
package main
import (
"log"
"os"
"strings"
)
type conf struct {
HttpRequestLog bool
HttpBindingAddress string
HttpsBindingAddress string
HttpsRedirectEnabled bool
TLSAutoDomain string
TLSCertFilepath string
TLSCertKeyFilepath string
VaultPrefix string
}
const HttpRequestLogVarenv = "OTS_HTTP_REQUEST_LOG"
const HttpBindingAddressVarenv = "OTS_HTTP_BINDING_ADDRESS"
const HttpsBindingAddressVarenv = "OTS_HTTPS_BINDING_ADDRESS"
const HttpsRedirectEnabledVarenv = "OTS_HTTPS_REDIRECT_ENABLED"
const TLSAutoDomainVarenv = "OTS_TLS_AUTO_DOMAIN"
const TLSCertFilepathVarenv = "OTS_TLS_CERT_FILEPATH"
const TLSCertKeyFilepathVarenv = "OTS_TLS_CERT_KEY_FILEPATH"
const VaultPrefixenv = "OTS_VAULT_PREFIX"
func loadConfig() conf {
var cnf conf
cnf.HttpRequestLog = strings.ToLower(os.Getenv(HttpRequestLogVarenv)) == "true"
cnf.HttpBindingAddress = os.Getenv(HttpBindingAddressVarenv)
cnf.HttpsBindingAddress = os.Getenv(HttpsBindingAddressVarenv)
cnf.HttpsRedirectEnabled = strings.ToLower(os.Getenv(HttpsRedirectEnabledVarenv)) == "true"
cnf.TLSAutoDomain = os.Getenv(TLSAutoDomainVarenv)
cnf.TLSCertFilepath = os.Getenv(TLSCertFilepathVarenv)
cnf.TLSCertKeyFilepath = os.Getenv(TLSCertKeyFilepathVarenv)
cnf.VaultPrefix = os.Getenv(VaultPrefixenv)
if cnf.TLSAutoDomain != "" && (cnf.TLSCertFilepath != "" || cnf.TLSCertKeyFilepath != "") {
log.Fatalf("Auto TLS (%s) is mutually exclusive with manual TLS (%s and %s)", TLSAutoDomainVarenv,
TLSCertFilepathVarenv, TLSCertKeyFilepathVarenv)
}
if (cnf.TLSCertFilepath != "" && cnf.TLSCertKeyFilepath == "") ||
(cnf.TLSCertFilepath == "" && cnf.TLSCertKeyFilepath != "") {
log.Fatalf("Both certificate filepath (%s) and certificate key filepath (%s) must be set when using manual TLS",
TLSCertFilepathVarenv, TLSCertKeyFilepathVarenv)
}
if cnf.HttpsBindingAddress == "" && (cnf.TLSAutoDomain != "" || cnf.TLSCertFilepath != "") {
log.Fatalf("HTTPS binding address (%s) must be set when using either auto TLS (%s) or manual TLS (%s and %s)",
HttpsBindingAddressVarenv, TLSAutoDomainVarenv, TLSCertFilepathVarenv, TLSCertKeyFilepathVarenv)
}
if cnf.HttpBindingAddress == "" && cnf.TLSAutoDomain == "" && cnf.TLSCertFilepath == "" {
log.Fatalf("HTTP binding address (%s) must be set if auto TLS (%s) and manual TLS (%s and %s) are both disabled",
HttpBindingAddressVarenv, TLSAutoDomainVarenv, TLSCertFilepathVarenv, TLSCertKeyFilepathVarenv)
}
if cnf.HttpsBindingAddress != "" && cnf.TLSAutoDomain == "" && cnf.TLSCertFilepath == "" {
log.Fatalf("HTTPS binding address (%s) is set but neither auto TLS (%s) nor manual TLS (%s and %s) are enabled",
HttpsBindingAddressVarenv, TLSAutoDomainVarenv, TLSCertFilepathVarenv, TLSCertKeyFilepathVarenv)
}
if cnf.VaultPrefix == "" {
cnf.VaultPrefix = "cubbyhole/"
}
log.Println("[INFO] HTTP Request Log enabled:", cnf.HttpRequestLog)
log.Println("[INFO] HTTP Binding Address:", cnf.HttpBindingAddress)
log.Println("[INFO] HTTPS Binding Address:", cnf.HttpsBindingAddress)
log.Println("[INFO] HTTPS Redirect enabled:", cnf.HttpsRedirectEnabled)
log.Println("[INFO] TLS Auto Domain:", cnf.TLSAutoDomain)
log.Println("[INFO] TLS Cert Filepath:", cnf.TLSCertFilepath)
log.Println("[INFO] TLS Cert Key Filepath:", cnf.TLSCertKeyFilepath)
log.Println("[INFO] Vault prefix:", cnf.VaultPrefix)
return cnf
}