-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
84 lines (70 loc) · 1.77 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
package main
import (
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"text/template"
"time"
"github.com/docker/docker/client"
)
var cli *client.Client
var apiKey string //nolint
var hassioNetwork *net.IPNet
var indexTemplate *template.Template
var wwwRoot string
var development bool
var httpClient http.Client
func main() {
var err error
cli, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
// Generate Network check
networkMask := "172.30.32.0/23"
if env := os.Getenv("NETWORK_MASK"); env != "" {
networkMask = env
}
_, hassioNetwork, _ = net.ParseCIDR(networkMask)
// system settings
apiKey = os.Getenv("SUPERVISOR_TOKEN")
development = (os.Getenv("DEVELOPMENT") == "True")
httpClient = http.Client{Timeout: 3 * time.Second}
if development {
wwwRoot = "./rootfs/usr/share/www/"
} else {
wwwRoot = "/usr/share/www/"
}
indexTemplate = template.Must(template.ParseFiles(wwwRoot + "/index.html"))
// API setup
http.HandleFunc("/", statusIndex)
http.HandleFunc("/ping", apiPing)
http.HandleFunc("/logs", apiLogs)
// Serve static help files
staticFiles := http.FileServer(http.Dir(wwwRoot))
http.Handle("/static/styles.css", staticFiles)
http.Handle("/static/scripts.js", staticFiles)
http.Handle("/static/tsparticles.min.js", staticFiles)
http.Handle("/static/Roboto-Regular.woff2", staticFiles)
// Run webserver
go func() {
log.Print("Start webserver on http://0.0.0.0:80")
if err := http.ListenAndServe(":80", nil); err != nil {
log.Fatal(err)
}
}()
signalChannel := make(chan os.Signal, 2)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
for {
sig := <-signalChannel
switch sig {
case os.Interrupt:
return
case syscall.SIGTERM:
return
}
}
}