forked from zneix/ServerGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (114 loc) · 3.21 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"fmt"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"
"github.com/mitchellh/panicwrap"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"github.com/SevenTV/ServerGo/src/configure"
"github.com/SevenTV/ServerGo/src/discord"
"github.com/SevenTV/ServerGo/src/mongo"
"github.com/SevenTV/ServerGo/src/mongo/cache"
"github.com/SevenTV/ServerGo/src/mongo/datastructure"
_ "github.com/SevenTV/ServerGo/src/redis"
"github.com/SevenTV/ServerGo/src/server"
"github.com/SevenTV/ServerGo/src/server/api/actions"
"github.com/SevenTV/ServerGo/src/server/api/tasks"
)
func init() {
log.Infoln("starting")
}
func main() {
// Catch panics - send alert to discord channel optionally
exitStatus, err := panicwrap.BasicWrap(panicHandler)
if err != nil {
// PANIC-CEPTION. BRRRRRRRRRRRRRRRRRRRRRRRR
panic(err)
}
if exitStatus >= 0 {
os.Exit(exitStatus)
}
configCode := configure.Config.GetInt("exit_code")
if configCode > 125 || configCode < 0 {
log.WithField("requested_exit_code", configCode).Warn("invalid exit code specified in config using 0 as new exit code")
configCode = 0
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
s := server.New()
go func() {
sig := <-c
log.WithField("sig", sig).Info("stop issued")
start := time.Now().UnixNano()
// Run pre-shutdown cleanup
Cleanup()
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
if err := s.Shutdown(); err != nil {
log.WithError(err).Error("failed to shutdown server")
}
}()
wg.Wait()
log.WithField("duration", float64(time.Now().UnixNano()-start)/10e5).Infof("shutdown")
os.Exit(configCode)
}()
go func() {
for {
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.WithField("alloc", m.Alloc).WithField("total_alloc", m.TotalAlloc).WithField("sys", m.Sys).WithField("numgc", m.NumGC).Debug("stats")
time.Sleep(5 * time.Second)
}
}()
log.Infoln("started")
// Get and cache roles*
ctx := context.Background()
roles, err := GetAllRoles(ctx)
if err != nil {
log.WithError(err).Error("could not get roles")
}
log.WithField("count", len(roles)).Infof("retrieved roles")
// Sync bans
err = actions.Bans.FetchBans(ctx)
if err != nil {
log.WithError(err).Error("could not sync bans")
}
log.WithField("count", len(actions.Bans.BannedUsers)).Info("retrieved bans")
go tasks.Start()
select {}
}
func Cleanup() {
// Cleanup ongoing tasks
tasks.Cleanup()
// Logout from discord
_ = discord.Discord.CloseWithCode(1000)
}
// Get all roles available and cache into the mongo context
func GetAllRoles(ctx context.Context) ([]datastructure.Role, error) {
roles := []datastructure.Role{}
cur, err := mongo.Collection(mongo.CollectionNameRoles).Find(ctx, bson.M{})
if err != nil {
return nil, err
}
roles = append(roles, *datastructure.DefaultRole) // Add default role
if err := cur.All(ctx, &roles); err != nil { // Fetch roles
return nil, err
}
// Set "AllRoles" value to mongo context
cache.CachedRoles = roles
return roles, nil
}
func panicHandler(output string) {
fmt.Printf("PANIC OCCURED:\n\n%s\n", output)
// Try to send a message to discord
discord.SendPanic(output)
os.Exit(1)
}