-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
69 lines (53 loc) · 1.35 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
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"strconv"
"time"
rice "github.com/GeertJohan/go.rice"
"github.com/gobuffalo/envy"
"github.com/sirupsen/logrus"
)
var port = flag.Int("p", 8080, "Port to use")
var intervalCli = flag.Int("i", 0, "Interval in minutes")
var configPath = flag.String("f", "./board.yaml", "Path to config file")
var log = logrus.StandardLogger()
func main() {
flag.Parse()
interval := getInterval()
log.Infof("Probe interval: %d", interval)
manager, err := loadConfig(parseConfigString(*configPath))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Setup static folder
http.Handle("/", http.FileServer(rice.MustFindBox("static").HTTPBox()))
// Setup logic route
http.HandleFunc("/data", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
data, _ := json.Marshal(manager)
_, _ = w.Write(data)
})
go manager.ProbeLoop(time.Duration(int64(interval)) * time.Minute)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*port), nil))
}
func getInterval() int {
intervalEnv := getInt(envy.Get("INTERVAL", ""))
if *intervalCli != 0 {
return *intervalCli
} else if intervalEnv != 0 {
return intervalEnv
}
return 10
}
func getInt(s string) int {
i, err := strconv.ParseInt(s, 10, 0)
if nil != err {
return 0
}
return int(i)
}