-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (98 loc) · 2.33 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
110
111
112
113
114
115
116
117
118
119
package main
import (
"encoding/json"
"github.com/BurntSushi/toml"
"github.com/go-telegram-bot-api/telegram-bot-api"
"log"
"math/rand"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
var (
logErr = log.New(os.Stderr, "[ERRO] ", log.Ldate+log.Ltime+log.Ltime+log.Lshortfile)
logWarn = log.New(os.Stdout, "[WARN] ", log.Ldate+log.Ltime)
logInfo = log.New(os.Stdout, "[INFO] ", log.Ldate+log.Ltime)
g = global{shutdown: make(chan bool),
games: make(map[chatID][]HangGame)}
sendMessageChan = make(chan tgbotapi.MessageConfig, 128)
)
func main() {
/////////////
// STARTUP
//////////////
// Parse settings file
_, err := toml.DecodeFile("settings.toml", &g.c)
if err != nil {
logErr.Println(err)
return
}
// Seed the RNG
rand.Seed(time.Now().UnixNano())
// Create new bot
g.bot, err = tgbotapi.NewBotAPI(g.c.Apikey)
if err != nil {
logErr.Println(err)
}
logInfo.Printf("Running as @%s", g.bot.Self.UserName)
// Create waitgroup, for synchronized shutdown
var wg sync.WaitGroup
g.wg = &wg
// Create the lock for the stats object
var gamesLock sync.RWMutex
g.gamesLock = &gamesLock
// Fill subscriptions object
err = Load("data.gob", &g.games)
if err != nil {
logErr.Println(err)
}
// All messages are received by the messageHandler
wg.Add(1)
go messageHandler()
for i := 0; i < 3; i++ { // Start 3 async message senders
wg.Add(1)
go messageSender(i)
}
wg.Add(1)
go dataSaver()
// Perform other startup tasks
sigs := make(chan os.Signal, 2)
signal.Notify(sigs, os.Interrupt, syscall.SIGINT)
time.Sleep(time.Millisecond)
logInfo.Println("All routines have been started, awaiting kill signal")
///////////////
// SHUTDOWN
///////////////
// Program will hang here
// On this select statement
select {
case <-sigs:
close(g.shutdown)
case <-g.shutdown:
}
println()
logInfo.Println("Shutdown signal received. Waiting for goroutines")
// Shutdown after all goroutines have exited
g.wg.Wait()
logWarn.Println("Shutting down")
}
func Save(path string, object interface{}) error {
file, err := os.Create(path)
if err == nil {
encoder := json.NewEncoder(file)
encoder.Encode(object)
}
file.Close()
return err
}
func Load(path string, o interface{}) error {
file, err := os.Open(path)
if err == nil {
dec := json.NewDecoder(file)
err = dec.Decode(o)
}
file.Close()
return err
}