-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
109 lines (93 loc) · 2.36 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
package main
import (
"log"
"os"
"path/filepath"
"strings"
"github.com/spf13/viper"
)
type Policy struct {
Enabled bool
Path string
Version string
Mode string
Mx []string
MaxAge int64
}
type ReportsMax struct {
Body int64
Json int64
}
type ReportsSave struct {
Enabled bool
Path string
}
type Reports struct {
Enabled bool
Path string
Max ReportsMax
Save ReportsSave
}
type ConfigMetrics struct {
Enabled bool
Port uint16
Path string
Collectors struct {
Go bool
Process bool
Exporter bool
}
}
type Config struct {
Port uint16
Log struct {
Json bool
}
Policy Policy
Reports Reports
Metrics ConfigMetrics
}
func createConfig() Config {
path := getEnv("CONFIG_PATH", "/etc/mta-sts-exporter/config.yaml")
directory := filepath.Dir(path)
fileName := filepath.Base(path)
extension := filepath.Ext(fileName)
configName := strings.TrimSuffix(fileName, extension)
if extension == "" {
viper.SetConfigType("yaml")
}
viper.SetConfigName(configName)
viper.AddConfigPath(directory)
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetDefault("Log.Json", true)
viper.SetDefault("Port", 8080)
viper.SetDefault("Policy.Enabled", true)
viper.SetDefault("Policy.Path", "/.well-known/mta-sts.txt")
viper.SetDefault("Policy.Version", "STSv1")
viper.SetDefault("Policy.Mode", "enforce")
viper.SetDefault("Policy.Mx", []string{"mx1.example.com", "mx2.example.com"})
viper.SetDefault("Policy.MaxAge", "86400")
viper.SetDefault("Reports.Enabled", true)
viper.SetDefault("Reports.Path", "/report")
viper.SetDefault("Reports.Max.Body", 1*1024*1024)
viper.SetDefault("Reports.Max.Json", 5*1024*1024)
viper.SetDefault("Reports.Save.Enabled", false)
viper.SetDefault("Reports.Save.Path", "/tmp/reports")
viper.SetDefault("Metrics.Enabled", true)
viper.SetDefault("Metrics.Port", 8081)
viper.SetDefault("Metrics.Path", "/metrics")
viper.SetDefault("Metrics.Collectors.Go", false)
viper.SetDefault("Metrics.Collectors.Process", false)
viper.SetDefault("Metrics.Collectors.Exporter", false)
if _, err := os.Stat(path); err == nil {
if err := viper.ReadInConfig(); err != nil {
log.Fatalln("Failed to read config file:", err)
}
}
var config Config
if err := viper.Unmarshal(&config); err != nil {
log.Fatalln("Failed to unmarshal config:", err)
}
return config
}