This repository has been archived by the owner on Dec 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
114 lines (95 loc) · 2.67 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
package incognitomail
import (
"errors"
"io"
"os"
"gopkg.in/gcfg.v1"
)
type generalConfig struct {
MailSystem string
UnixSockPath string
LockFilePath string
ListenPath string
ListenAddress string
TLSCertFile string
TLSKeyFile string
}
type persistenceConfig struct {
Type string
DatabasePath string
}
type postfixConfig struct {
Domain string
MapFilePath string
}
type config struct {
General generalConfig
Persistence persistenceConfig
PostfixConfig postfixConfig
}
var (
defaultConfig = config{
General: generalConfig{
MailSystem: "postfix",
UnixSockPath: "/tmp/incognitomail.sock",
LockFilePath: "/var/lock/incognitomail.lock",
ListenPath: "/incognitomail",
ListenAddress: ":8080",
TLSCertFile: "",
TLSKeyFile: "",
},
Persistence: persistenceConfig{
Type: "boltdb",
DatabasePath: "incognitomail.db",
},
PostfixConfig: postfixConfig{
Domain: "",
MapFilePath: "",
},
}
// Config holds all global configuration.
Config = defaultConfig
// ErrInvalidConfig is used when loading a configuration with invalid values.
ErrInvalidConfig = errors.New("invalid configuration values")
)
// ResetConfig switches all values back to the default.
func ResetConfig() {
Config = defaultConfig
}
// ReadConfigFromFile reads the file in the given path and parses all config data from it. Any value not defined in this configuration file will be kept as its default value.
func ReadConfigFromFile(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
err = ReadConfigFromReader(f)
return err
}
// ReadConfigFromReader parses all config data from the given reader. Any value not defined in the read string will be kept as its default value.
func ReadConfigFromReader(reader io.Reader) error {
err := gcfg.ReadInto(&Config, reader)
if err != nil {
return err
}
if !ValidConfig() {
return ErrInvalidConfig
}
return nil
}
// ValidConfig returns true if the current Config is valid, i.e. not likely to crash the server.
func ValidConfig() bool {
invalid := false
invalid = invalid || Config.General.MailSystem != "postfix"
invalid = invalid || Config.General.UnixSockPath == ""
invalid = invalid || Config.General.LockFilePath == ""
invalid = invalid || Config.General.ListenPath == ""
invalid = invalid || Config.General.ListenAddress == ""
invalid = invalid || Config.Persistence.Type != "boltdb"
invalid = invalid || Config.Persistence.DatabasePath == ""
if Config.General.MailSystem == "postfix" {
invalid = invalid || Config.PostfixConfig.Domain == ""
invalid = invalid || Config.PostfixConfig.MapFilePath == ""
}
return !invalid
}