-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (62 loc) · 1.47 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
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/pkg/errors"
"github.com/atomicptr/blackdesert-monitor/monitor"
)
func main() {
err := run()
if err != nil {
log.Fatal(err)
}
}
func run() error {
// logger
logger := log.New(os.Stdout, "", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
// config
filePath := "./settings.yaml"
// arguments found, try to use that as a path
if len(os.Args) > 1 {
logger.Printf("Arguments found: %v\n", os.Args[1:])
filePath = os.Args[1]
}
config, err := monitor.ConfigFromFile(filePath)
if err != nil {
return errors.Wrapf(err, "could not open file \"%s\"", filePath)
}
if err := config.Validate(); err != nil {
return err
}
if config.Telegram.UserId == 0 {
logger.Println("Your Telegram User ID is empty! You can get it by sending /myid to the bot!")
}
// channel to listen for errors coming from the app
appErrors := make(chan error, 1)
// app starting
logger.Printf("main: blackdesert-monitor starting...")
defer logger.Printf("main: Done")
// channel to listen for interrupt or terminate signal from OS
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
pm, err := monitor.New(
config,
logger,
)
if err != nil {
return err
}
go func() {
appErrors <- pm.Start()
}()
select {
case err := <-appErrors:
return errors.Wrap(err, "app error")
case sig := <-shutdown:
logger.Printf("main: %v shutdown...", sig)
pm.Stop()
}
return nil
}