-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.go
51 lines (45 loc) · 1.43 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
package main
import (
"encoding/json"
"os"
"time"
urapi "github.com/gbl08ma/uptimerobot-api"
)
// Config holds the application-wide settings
type Config struct {
MonitorComponentMap map[int][]int
MonitorMetricMap map[int][]int
MonitorComponentStatusMap map[urapi.MonitorStatus]int
CheckInterval time.Duration
CachetAPIkey string
CachetEndpoint string
UptimeRobotAPIkey string
BindAddress string
}
// NewConfig creates a Config with default settings
func NewConfig() *Config {
return &Config{
MonitorComponentMap: make(map[int][]int),
MonitorMetricMap: make(map[int][]int),
MonitorComponentStatusMap: map[urapi.MonitorStatus]int{
urapi.MonitorStatusDown: 4,
urapi.MonitorStatusUp: 1,
},
CachetAPIkey: os.Getenv("UPCACHET_CACHET_APIKEY"),
CachetEndpoint: os.Getenv("UPCACHET_CACHET_ENDPOINT"),
UptimeRobotAPIkey: os.Getenv("UPCACHET_UPTIMEROBOT_APIKEY"),
}
}
// Load loads a JSON-encoded Config from the specified filename
func (c *Config) Load(filename string) error {
file, _ := os.Open(filename)
decoder := json.NewDecoder(file)
return decoder.Decode(c)
}
// Save saves a JSON-encoded Config to the specified filename
func (c *Config) Save(filename string) error {
file, _ := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0664)
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
return encoder.Encode(c)
}