forked from pereiro/smtprelay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
76 lines (68 loc) · 1.54 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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"reflect"
)
type Conf struct {
ServerHostName string
ListenPort string
WelcomeMessage string
MaxIncomingConnections int
MaxOutcomingConnections int
DKIMEnabled bool
DKIMKeyDir string
RelayModeEnabled bool
RelayServer string
NumCPU int
StatisticPort string
DeferredMailDelay int
DeferredMailMaxErrors int
MaxRecipients int
ListenTCPPort string
TCPMaxConnections int
TCPMaxHandlers int
TCPTimeoutSeconds int
}
func (cf *Conf) Load(filename string) error {
file, err := ioutil.ReadFile(filename)
if err != nil {
log.Error("file error:%s", err)
return err
}
err = json.Unmarshal(file, &cf)
if err != nil {
log.Error("json parse error:%s", err)
return err
}
// err = cf.CheckConfig()
// if err != nil {
// log.Error("configuration isn't valid:%s",err.Error())
// }
return nil
}
func (cf *Conf) Save(filename string) error {
data, err := json.Marshal(cf)
if err != nil {
log.Error("json marshall error:%s\n,err")
return err
}
err = ioutil.WriteFile(filename, data, 0644)
if err != nil {
log.Error("file error")
return err
}
return nil
}
func (cf *Conf) CheckConfig() error {
ref := reflect.ValueOf(*cf)
for i := 0; i < ref.NumField(); i++ {
field := ref.Field(i)
if field.IsNil() {
return errors.New(fmt.Sprintf("no value found for %s", ref.Type().Field(i).Name))
}
}
return nil
}