/
main.go
54 lines (43 loc) · 1.22 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/namsral/flag"
)
func main() {
mqttHost := flag.String("mqtt-host", "mqtt", "MQTT hostname/address")
mqttPort := flag.Int("mqtt-port", 1883, "MQTT port")
wsPort := flag.Int("ws-port", 8081, "Websocket port")
noOriginCheck := flag.Bool("noOriginCheck", false, "Disable request origin check")
thermoCenterAPIHost := flag.String("thermo-center-api-host", "thermo-center-api", "Hostname of thermo-center-api component")
thermoCenterAPIPort := flag.Int("thermo-center-api-port", 8080, "Port of thermo-center-api component")
flag.Parse()
if *noOriginCheck {
upgrader.CheckOrigin = func(r *http.Request) bool {
return true
}
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hub := newHub(*thermoCenterAPIHost, *thermoCenterAPIPort)
go hub.run(ctx)
mqttclient := newMqttClient(hub, *mqttHost, *mqttPort)
go mqttclient.run(ctx)
httpServer := http.Server{
Addr: fmt.Sprintf(":%d", *wsPort),
Handler: hub,
}
go httpServer.ListenAndServe()
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGTERM)
<-sc
cancel()
if err := httpServer.Shutdown(ctx); err != nil {
log.Print(err)
}
}