-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
133 lines (104 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
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
package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
"sync"
"github.com/kpango/glg"
)
const (
BaseAddress = "127.0.0.1"
)
type Configuration struct {
Address string `json:"address"`
Port int `json:"port"`
MotionConfigFile string `json:"motionConfigFile"`
Username string `json:"username"`
Password string `json:"password"`
AppPath string `json:"appPath"`
Ssl SSL `json:"ssl"`
Backup Backup `json:"backup"`
Notify Notify `json:"notify"`
}
type SSL struct {
CertFile string `json:"cert"`
KeyFile string `json:"key"`
}
type Backup struct {
When string `json:"when"`
Method string `json:"method"`
EncryptionKey string `json:"encryptionKey"`
Archive bool `json:"archive"`
FilePerArchive int `json:"filePerArchive"`
}
type Notify struct {
Method string `json:"method"`
Token string `json:"token"`
To []string `json:"to"`
Message string `json:"message"`
Photo int `json:"photo"`
}
var (
mu sync.Mutex
conf Configuration
)
// Load function convert a loaded JSON config file to a config struct
// return err if secret param is empty
func Load(filename string) error {
mu.Lock()
defer mu.Unlock()
glg.Infof("Loading configuration from %s ...", filename)
if conf.IsEmpty() {
raw, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
err = json.Unmarshal(raw, &conf)
if err != nil {
return err
}
glg.Debugf("Current config: %+v", conf)
} else {
return fmt.Errorf("Configuration already loaded")
}
return nil
}
func Unload() {
mu.Lock()
defer mu.Unlock()
glg.Debug("Unloading configuration")
conf = Configuration{}
}
func GetConfig() Configuration {
mu.Lock()
defer mu.Unlock()
return conf
}
func GetSSLConfig() SSL {
mu.Lock()
defer mu.Unlock()
return conf.Ssl
}
func GetBackupConfig() Backup {
mu.Lock()
defer mu.Unlock()
return conf.Backup
}
func GetNotifyConfig() Notify {
mu.Lock()
defer mu.Unlock()
return conf.Notify
}
func (c Configuration) IsEmpty() bool {
return reflect.DeepEqual(c, Configuration{})
}
func (c SSL) IsEmpty() bool {
return reflect.DeepEqual(c, SSL{})
}
func (c Notify) IsEmpty() bool {
return reflect.DeepEqual(c, Notify{})
}
func (c Backup) IsEmpty() bool {
return reflect.DeepEqual(c, Backup{})
}