-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
149 lines (123 loc) · 3.2 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
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
package ocrmymail
import (
"fmt"
"os"
"strconv"
)
var (
defaultMetricsAddress = ":9090"
defaultSMTPHost = "localhost"
defaultSMTPPort = "25"
)
// Config contains all the config for serving OCRMyMail
type Config struct {
MetricsAddress string
AccessLog bool
HandleAsync bool
SentryDSN string
// SMTP server config for OCRMyMail
SMTP struct {
Host string
Port int
FromEmail string
FromName string
}
// Remote SMTP to which emails will be relayed
RemoteSMTP struct {
Host string
Port int
User string
Password string
DisableTLS bool
}
AdminMail string
}
// BuildConfigFromEnv populates a config from env variables
func BuildConfigFromEnv() *Config {
config := &Config{}
// OCRMyMail SMTP settings
config.SMTP.Host = getEnv("SMTP_HOST", defaultSMTPHost)
port, err := strconv.Atoi(getEnv("SMTP_PORT", defaultSMTPPort))
if err != nil {
config.SMTP.Port = 0
} else {
config.SMTP.Port = port
}
config.SMTP.FromName = getEnv("SMTP_FROM_NAME", "")
config.SMTP.FromEmail = getEnv("SMTP_FROM_EMAIL", "")
if config.SMTP.FromEmail != "" && config.SMTP.FromName != "" {
config.SMTP.FromEmail = fmt.Sprintf("%s <%s>", config.SMTP.FromName, config.SMTP.FromEmail)
}
// Remote SMTP server settings
config.RemoteSMTP.Host = getEnv("REMOTE_SMTP_HOST", "")
port, err = strconv.Atoi(getEnv("REMOTE_SMTP_PORT", "0"))
if err != nil {
config.RemoteSMTP.Port = 0
} else {
config.RemoteSMTP.Port = port
}
config.RemoteSMTP.User = getEnv("REMOTE_SMTP_USER", "")
config.RemoteSMTP.Password = getEnv("REMOTE_SMTP_PASSWORD", "")
if getEnv("REMOTE_SMTP_DISABLE_TLS", "0") == "1" {
config.RemoteSMTP.DisableTLS = true
}
// Access log
accessLog := getEnv("ACCESS_LOG", "1")
if accessLog == "0" {
config.AccessLog = false
} else {
config.AccessLog = true
}
// Handle Async
handleAsync := getEnv("HANDLE_ASYNC", "0")
if handleAsync == "1" {
config.HandleAsync = true
} else {
config.HandleAsync = false
}
// Admin email
config.AdminMail = getEnv("ADMIN_MAIL", "")
// Metrics
config.MetricsAddress = getEnv("METRICS_ADDRESS", defaultMetricsAddress)
// Sentry DSN
config.SentryDSN = getEnv("SENTRY_DSN", "")
return config
}
// Validate validates whether all config is set and valid
func (config *Config) Validate() error {
// SMTP config
if config.SMTP.Host == "" {
return fmt.Errorf("SMTP_HOST must be set")
}
if config.SMTP.Port == 0 {
return fmt.Errorf("SMTP_PORT must be set")
}
if config.SMTP.FromEmail == "" {
return fmt.Errorf("REMOTE_SMTP_FROM_EMAIL must be set")
}
// Remote SMTP config
if config.RemoteSMTP.Host == "" {
return fmt.Errorf("REMOTE_SMTP_HOST must be set")
}
if config.RemoteSMTP.Port == 0 {
return fmt.Errorf("REMOTE_SMTP_PORT must be set")
}
// Admin mail
if config.AdminMail == "" {
return fmt.Errorf("ADMIN_MAIL must be set")
}
// Metrics
if config.MetricsAddress == "" {
return fmt.Errorf("METRICS_ADDRESS cannot be empty")
}
return nil
}
// getEnv gets the env variable with the given key if the key exists
// else it falls back to the fallback value
func getEnv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}