-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (76 loc) · 1.92 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"os"
"strconv"
"sync"
"github.com/Ordspilleren/ChangeMonitor/monitor"
"github.com/Ordspilleren/ChangeMonitor/notify"
"github.com/Ordspilleren/ChangeMonitor/storage"
)
var wg = &sync.WaitGroup{}
var ConfigFile string
var StorageDirectory string
var ChromePath string
var ChromeWs string
var EnableWebUI bool
type Config struct {
Monitors monitor.Monitors `json:"monitors"`
Notifiers notify.Notifiers `json:"notifiers"`
}
var config Config
var monitorService *monitor.MonitorService
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func init() {
ConfigFile = getEnv("CONFIG_FILE", "config.json")
StorageDirectory = getEnv("STORAGE_DIRECTORY", "data")
ChromePath = getEnv("CHROME_PATH", "/usr/bin/chromium")
ChromeWs = getEnv("CHROME_WS", "")
EnableWebUI, _ = strconv.ParseBool(getEnv("ENABLE_WEBUI", "false"))
log.Printf("Config File: %s", ConfigFile)
log.Printf("Storage Directory: %s", StorageDirectory)
b, err := ioutil.ReadFile(ConfigFile)
if err != nil {
log.Print(err)
return
}
err = json.Unmarshal(b, &config)
if err != nil {
log.Print(err)
return
}
notifierService := notify.NewNotifierService(config.Notifiers)
storage := storage.InitStorage(StorageDirectory)
monitorService = monitor.NewMonitorService(wg, config.Monitors, storage, notifierService)
if ChromeWs != "" {
monitorService.NewMonitorClients(ChromeWs, true)
} else {
monitorService.NewMonitorClients(ChromePath, false)
}
monitorService.InitMonitors()
}
func main() {
monitorService.StartMonitoring()
if EnableWebUI {
server := server{}
server.start()
} else {
wg.Wait()
}
}
func (t *Config) JSON() ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", "\t")
err := encoder.Encode(t)
return buffer.Bytes(), err
}