-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
109 lines (96 loc) · 2.51 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
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
//go:generate qtc -dir=templates
package main
import (
"flag"
"log"
"os"
"time"
"github.com/joho/godotenv"
"github.com/valyala/fasthttp"
)
//goland:noinspection HttpUrlsUsage
var (
debug = flag.Bool("debug", false, "Whether to print debug output")
authTokenFlag = flag.String("token", "", "An alternative token to be used when debugging")
protocol = "http://" // set in .env
addr = "0.0.0.0:6060" // set in .env
serverName = "A Development Build" // set in .env
authToken = "" // set in .env // TODO: Add support for multi tokens
gitCommitHash = "<unknown>" // This is changed with compile flags in Makefile
timeFormat = "Jan 02 2006 15:04:05 MST"
uptimeTimer = time.Now().UnixMilli()
)
func main() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
// Setup flags and config
flag.Parse()
setupEnv()
// Setup DB and load values
rdb, rjh = SetupDatabase()
SetupLocalValues()
go SetupDatabaseSaving()
log.Printf("- Running heartbeat on " + protocol + addr)
h := RequestHandler
h = fasthttp.CompressHandler(h)
if err := fasthttp.ListenAndServe(addr, h); err != nil {
log.Fatalf("- Error in ListenAndServe: %s", err)
}
defer func() {
if err := rdb.Close(); err != nil {
log.Fatalf("goredis - failed to communicate to redis-server: %v", err)
}
}()
}
func setupEnv() {
err := godotenv.Load("config/.env")
if err != nil {
log.Fatalf("Fatal error: %v", err)
return
}
if *debug && len(*authTokenFlag) > 0 {
authToken = *authTokenFlag
} else {
if ev := os.Getenv("HB_TOKEN"); len(ev) == 0 {
log.Fatalf("HB_TOKEN not set in config/.env")
return
} else {
authToken = ev
}
}
if ev := os.Getenv("HB_PROTOCOL"); len(ev) > 0 {
protocol = ev
}
if ev := os.Getenv("HB_ADDR"); len(ev) > 0 {
addr = ev
}
if ev := os.Getenv("HB_SERVER_NAME"); len(ev) > 0 {
serverName = ev
}
if ev := os.Getenv("HB_GITHUB_LINK"); len(ev) > 0 {
gitRepo = ev
}
if ev := os.Getenv("HB_WEBHOOK_URL"); len(ev) > 0 {
webhookUrl = ev
}
if ev := os.Getenv("HB_WEBHOOK_LEVEL"); len(ev) > 0 {
switch ev {
case "ALL":
webhookLevel = WebhookLevelAll
case "SIMPLE":
webhookLevel = WebhookLevelSimple
case "LONG_ABSENCE":
webhookLevel = WebhookLevelLongAbsence
default:
webhookLevel = WebhookLevelNone
}
}
if ev := os.Getenv("HB_LIVE_URL"); len(ev) > 0 {
liveUrl = ev
}
if ev := os.Getenv("REDIS_ADDR"); len(ev) > 0 {
redisAddr = ev
}
if ev := os.Getenv("REDIS_PASS"); len(ev) > 0 {
redisPass = ev
}
}