-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (52 loc) · 1.55 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
package main
import (
"net/http"
"os"
"github.com/gorilla/mux"
config "github.com/kennedn/restate-go/internal/common/config"
"github.com/kennedn/restate-go/internal/common/logging"
"github.com/kennedn/restate-go/internal/device"
"github.com/kennedn/restate-go/internal/mqtt/frigate"
"github.com/kennedn/restate-go/internal/router"
"gopkg.in/yaml.v3"
)
func main() {
envConfigPath := os.Getenv("RESTATECONFIG")
configBytes, err := os.ReadFile(envConfigPath)
if err != nil {
logging.Log(logging.Error, "Could not read config path (RESTATECONFIG=%s)", envConfigPath)
os.Exit(1)
}
configMap := config.Config{}
if err := yaml.Unmarshal(configBytes, &configMap); err != nil {
logging.Log(logging.Error, "Could not parse config path (RESTATECONFIG=%s)", envConfigPath)
os.Exit(1)
}
devices := &device.Devices{}
routes, err := devices.Routes(&configMap)
var r *mux.Router
if err != nil {
logging.Log(logging.Info, err.Error())
} else {
r = router.NewRouter(routes)
if r == nil {
logging.Log(logging.Error, "Failed to create router")
os.Exit(1)
}
}
frigate := &frigate.Device{}
listeners, err := frigate.Listeners(&configMap)
if err != nil {
logging.Log(logging.Info, err.Error())
} else {
for _, listener := range listeners {
listener.Listen()
}
}
if len(routes) == 0 && len(listeners) == 0 {
logging.Log(logging.Error, "No devices or listeners provided, nothing left to do")
os.Exit(1)
}
logging.Log(logging.Info, "Server listening on :8080")
logging.Log(logging.Error, http.ListenAndServe(":8080", r).Error())
}