-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
101 lines (80 loc) · 2.33 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
package main
import (
"os"
log "github.com/sirupsen/logrus"
)
// Config is the configuration for the application.
type Config struct {
// UserliToken is the token for the userli service.
UserliToken string
// UserliBaseURL is the base URL for the userli service.
UserliBaseURL string
// AliasListenAddr is the address to listen for alias requests.
AliasListenAddr string
// DomainListenAddr is the address to listen for domain requests.
DomainListenAddr string
// MailboxListenAddr is the address to listen for mailbox requests.
MailboxListenAddr string
// SendersListenAddr is the address to listen for senders requests.
SendersListenAddr string
// MetricsListenAddr is the address to listen for metrics requests.
MetricsListenAddr string
}
// NewConfig creates a new Config with default values.
func NewConfig() *Config {
logLevel := os.Getenv("LOG_LEVEL")
if logLevel == "" {
logLevel = "info"
}
logFormat := os.Getenv("LOG_FORMAT")
if logFormat == "" {
logFormat = "text"
}
level, err := log.ParseLevel(logLevel)
if err != nil {
log.WithError(err).Fatal("Failed to parse log level")
}
log.SetLevel(level)
if logFormat == "json" {
log.SetFormatter(&log.JSONFormatter{})
} else {
log.SetFormatter(&log.TextFormatter{})
}
userliBaseURL := os.Getenv("USERLI_BASE_URL")
if userliBaseURL == "" {
userliBaseURL = "http://localhost:8000"
}
userliToken := os.Getenv("USERLI_TOKEN")
if userliToken == "" {
log.Fatal("USERLI_TOKEN is required")
}
aliasListenAddr := os.Getenv("ALIAS_LISTEN_ADDR")
if aliasListenAddr == "" {
aliasListenAddr = ":10001"
}
domainListenAddr := os.Getenv("DOMAIN_LISTEN_ADDR")
if domainListenAddr == "" {
domainListenAddr = ":10002"
}
mailboxListenAddr := os.Getenv("MAILBOX_LISTEN_ADDR")
if mailboxListenAddr == "" {
mailboxListenAddr = ":10003"
}
sendersListenAddr := os.Getenv("SENDERS_LISTEN_ADDR")
if sendersListenAddr == "" {
sendersListenAddr = ":10004"
}
metricsListenAddr := os.Getenv("METRICS_LISTEN_ADDR")
if metricsListenAddr == "" {
metricsListenAddr = ":10005"
}
return &Config{
UserliBaseURL: userliBaseURL,
UserliToken: userliToken,
AliasListenAddr: aliasListenAddr,
DomainListenAddr: domainListenAddr,
MailboxListenAddr: mailboxListenAddr,
SendersListenAddr: sendersListenAddr,
MetricsListenAddr: metricsListenAddr,
}
}