-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.go
67 lines (59 loc) · 1.47 KB
/
app.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
package main
import (
"context"
"fmt"
"grafana-matrix-forwarder/cfg"
"grafana-matrix-forwarder/matrix"
"grafana-matrix-forwarder/server"
"log"
"os"
"os/signal"
)
var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
)
func main() {
ctx, _ := listenForInterrupt()
appSettings := cfg.Parse()
if appSettings.VersionMode {
printAppVersion()
} else {
err := run(ctx, appSettings)
if err != nil {
log.Fatal(err)
}
log.Print("done")
}
}
func listenForInterrupt() (context.Context, context.CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
go func() {
oscall := <-c
log.Printf("system call: %+v", oscall)
cancel()
}()
return ctx, cancel
}
func printAppVersion() {
fmt.Println(version)
fmt.Printf(" build date: %s\r\n commit hash: %s\r\n built by: %s\r\n", date, commit, builtBy)
}
func run(ctx context.Context, appSettings cfg.AppSettings) error {
log.Print("starting matrix client ...")
var err error
var writeCloser matrix.WriteCloser
if len(appSettings.UserToken) > 0 {
writeCloser, err = matrix.NewMatrixWriteCloserWithToken(appSettings.UserID, appSettings.UserToken, appSettings.HomeserverURL)
} else {
writeCloser, err = matrix.NewMatrixWriteCloser(appSettings.UserID, appSettings.UserPassword, appSettings.HomeserverURL)
}
if err != nil {
return err
}
return server.BuildServer(ctx, writeCloser, appSettings).Start()
}