-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
85 lines (67 loc) · 1.42 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
/*
gofra is an XMPP bot engine.
*/
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"gopkg.in/yaml.v3"
"github.com/XaviFP/gofra/internal"
)
var config gofra.Config
var g *gofra.Gofra
func init() {
configFilePathPtr := flag.String("config", "config.yaml", "file path of the config.yml file")
flag.Parse()
loadConfig(*configFilePathPtr)
}
func loadConfig(configFilePath string) {
yamlFile, err := ioutil.ReadFile(configFilePath)
if err != nil {
log.Fatalln("Error reading config file", err)
}
if err := yaml.Unmarshal(yamlFile, &config); err != nil {
log.Fatalf("Unmarshal: %v", err)
}
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Handle SIGINT and gracefully shut down the bot.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
select {
case <-ctx.Done():
case <-c:
cancel()
}
}()
g = gofra.NewGofra(ctx, config)
defer func() {
g.Logger.Info("Closing conn…")
if err := g.Client.Conn().Close(); err != nil {
g.Logger.Error(fmt.Sprintf("Error closing connection: %q", err))
}
}()
go func() {
<-ctx.Done()
g.Logger.Info("Closing session…")
if err := g.Client.Close(); err != nil {
g.Logger.Error(fmt.Sprintf("Error closing session: %q", err))
}
}()
err := g.Init()
if err != nil {
log.Fatal(err.Error())
}
err = g.Connect()
if err != nil {
log.Fatal(err.Error())
}
}