-
Notifications
You must be signed in to change notification settings - Fork 4
/
marvin.go
114 lines (98 loc) · 2.92 KB
/
marvin.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
package main
import (
"crypto/tls"
"flag"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"github.com/nogiushi/marvin/actions"
"github.com/nogiushi/marvin/activity"
"github.com/nogiushi/marvin/ambientlight"
"github.com/nogiushi/marvin/daylights"
"github.com/nogiushi/marvin/hue"
"github.com/nogiushi/marvin/lightstates"
"github.com/nogiushi/marvin/motion"
"github.com/nogiushi/marvin/nightlights"
"github.com/nogiushi/marvin/nog"
"github.com/nogiushi/marvin/nouns"
"github.com/nogiushi/marvin/persist"
"github.com/nogiushi/marvin/presence"
"github.com/nogiushi/marvin/schedule"
)
var Root *string
func main() {
config := flag.String("config", "/etc/marvin.json", "file path to configuration file")
address := flag.String("address", ":9999", "http service address")
cert := flag.String("cert", "", "certificate file")
key := flag.String("key", "", "key file")
Root = flag.String("root", "/usr/share/marvin", "...")
flag.Parse()
nog.Root = *Root
ReadVersion()
log.Println("starting marvin")
n := nog.NewNog()
if j, err := os.OpenFile(*config, os.O_RDONLY, 0666); err == nil {
if err = n.Load(j); err != nil {
panic(err)
}
j.Close()
} else {
log.Println("ERROR: could not open config:", err)
}
n.Register("Actions", actions.Handler)
n.Register("Activity", activity.Handler)
n.Register("Ambient Light", ambientlight.Handler)
n.Register("Daylights", daylights.Handler)
n.Register("Schedule", schedule.Handler)
n.Register("Lights", hue.Handler)
n.Register("Light States", lightstates.Handler)
n.Register("Presence", presence.Handler)
n.Register("Motion", motion.Handler)
n.Register("Nightlights", nightlights.Handler)
n.Register("Nouns", nouns.Handler)
p := &persist.Persist{}
go n.Register("Persist", p.Handler)
AddPersistenceHandlers(p)
AddHandlers(n)
go n.Run()
addresses := strings.Split(*address, ",")
for i, addr := range addresses {
if i == 1 || (len(addresses) == 1 && (*cert != "" || *key != "")) {
log.Println("starting secure:", addr, i)
go func(a string) {
config := &tls.Config{ClientAuth: tls.RequestClientCert}
server := &http.Server{Addr: a, TLSConfig: config}
if err := server.ListenAndServeTLS(*cert, *key); err != nil {
log.Print("ListenAndServe:", err)
}
}(addr)
} else {
log.Println("starting:", addr, i)
go func(a string) {
server := &http.Server{Addr: a}
err := server.ListenAndServe()
if err != nil {
log.Print("ListenAndServe:", err)
}
}(addr)
}
}
notifyChannel := make(chan os.Signal, 1)
signal.Notify(notifyChannel, os.Interrupt, syscall.SIGHUP, syscall.SIGTERM)
sig := <-notifyChannel
log.Println("handling:", sig)
if j, err := os.Create(*config); err == nil {
if err = n.Save(j); err == nil {
log.Println("saved:", *config)
} else {
log.Println("ERROR: saving config", err)
}
j.Close()
} else {
log.Println("ERROR: could not create config file:", err)
}
log.Println("stopping Marvin")
}