-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (48 loc) · 1.33 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
package main
import (
"flag"
"github.com/mwuertinger/hal/pkg/mqtt"
"log"
"os"
"os/signal"
"syscall"
"github.com/mwuertinger/hal/pkg/config"
"github.com/mwuertinger/hal/pkg/device"
"github.com/mwuertinger/hal/pkg/frontend"
"github.com/mwuertinger/hal/pkg/persistence"
)
func main() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, os.Kill, syscall.SIGTERM)
configPath := flag.String("config", "", "Path to config file.")
flag.Parse()
if len(*configPath) < 1 {
log.Fatalf("Missing -config argument.")
}
c, err := config.Load(*configPath)
if err != nil {
log.Fatalf("Failed to read config file: %v", err)
}
persistence := persistence.GetInMemoryService()
if err := persistence.Start(); err != nil {
log.Fatalf("persistence.Start: %v", err)
}
mqttBroker := mqtt.New()
if err := mqttBroker.Connect(c.Mqtt); err != nil {
log.Fatalf("mqttBroker.Connect: %v", err)
}
device.SetMqttBroker(mqttBroker)
if err := device.RegisterDevices(c.Devices); err != nil {
log.Fatalf("device.RegisterDevices: %v", err)
}
if err := frontend.Start(c.Http); err != nil {
log.Fatalf("frontend.Start: %v", err)
}
log.Println("Server ready")
// Wait for receiving a signal.
sig := <-sigc
log.Printf("Received %v signal, shutting down...", sig)
frontend.Shutdown()
mqttBroker.Shutdown()
device.Shutdown()
}