-
Notifications
You must be signed in to change notification settings - Fork 927
/
common.go
190 lines (148 loc) · 3.62 KB
/
common.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package common
//go:generate sqlboiler --no-hooks psql
import (
"database/sql"
"fmt"
"github.com/DataDog/datadog-go/statsd"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/jonas747/discordgo"
"github.com/mediocregopher/radix"
"github.com/sirupsen/logrus"
"github.com/volatiletech/sqlboiler/boil"
stdlog "log"
"os"
"strconv"
)
const (
VERSIONMAJOR = 1
VERSIONMINOR = 17
VERSIONPATCH = 2
)
var (
VERSIONNUMBER = fmt.Sprintf("%d.%d.%d", VERSIONMAJOR, VERSIONMINOR, VERSIONPATCH)
VERSION = VERSIONNUMBER
GORM *gorm.DB
PQ *sql.DB
RedisPool *radix.Pool
BotSession *discordgo.Session
BotUser *discordgo.User
Conf *CoreConfig
RedisPoolSize = 25
Statsd *statsd.Client
Testing = os.Getenv("YAGPDB_TESTING") != ""
CurrentRunCounter int64
NodeID string
_ interface{} = ensure64bit
)
// Initalizes all database connections, config loading and so on
func Init() error {
stdlog.SetOutput(&STDLogProxy{})
stdlog.SetFlags(0)
if Testing {
logrus.SetLevel(logrus.DebugLevel)
}
config, err := LoadConfig()
if err != nil {
return err
}
Conf = config
err = setupGlobalDGoSession()
if err != nil {
return err
}
ConnectDatadog()
err = connectRedis(config.Redis)
if err != nil {
return err
}
err = connectDB(config.PQHost, config.PQUsername, config.PQPassword, "yagpdb")
if err != nil {
panic(err)
}
BotUser, err = BotSession.UserMe()
if err != nil {
panic(err)
}
BotSession.State.User = &discordgo.SelfUser{
User: BotUser,
}
err = RedisPool.Do(radix.Cmd(&CurrentRunCounter, "INCR", "yagpdb_run_counter"))
if err != nil {
panic(err)
}
if !InitSchema(CoreServerConfDBSchema, "core configs") {
logrus.Fatal("error initializing schema")
}
return err
}
func setupGlobalDGoSession() (err error) {
BotSession, err = discordgo.New(Conf.BotToken)
if err != nil {
return err
}
maxCCReqs, _ := strconv.Atoi(os.Getenv("YAGPDB_MAX_CCR"))
if maxCCReqs < 1 {
maxCCReqs = 25
}
logrus.Info("max ccr set to: ", maxCCReqs)
BotSession.MaxRestRetries = 5
BotSession.Ratelimiter.MaxConcurrentRequests = maxCCReqs
return nil
}
func ConnectDatadog() {
if Conf.DogStatsdAddress == "" {
logrus.Warn("No datadog info provided, not connecting to datadog aggregator")
return
}
client, err := statsd.New(Conf.DogStatsdAddress)
if err != nil {
logrus.WithError(err).Error("Failed connecting to dogstatsd, datadog integration disabled")
return
}
if NodeID != "" {
client.Tags = append(client.Tags, "node:"+NodeID)
}
Statsd = client
currentTransport := BotSession.Client.HTTPClient.Transport
BotSession.Client.HTTPClient.Transport = &LoggingTransport{Inner: currentTransport}
}
func InitTest() {
testDB := os.Getenv("YAGPDB_TEST_DB")
if testDB == "" {
return
}
err := connectDB("localhost", "postgres", "123", testDB)
if err != nil {
panic(err)
}
}
func connectRedis(addr string) (err error) {
RedisPool, err = radix.NewPool("tcp", addr, RedisPoolSize, radix.PoolOnEmptyWait())
if err != nil {
logrus.WithError(err).Fatal("Failed intitializing redis pool")
}
return
}
func connectDB(host, user, pass, dbName string) error {
if host == "" {
host = "localhost"
}
db, err := gorm.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable password='%s'", host, user, dbName, pass))
GORM = db
PQ = db.DB()
boil.SetDB(PQ)
if err == nil {
PQ.SetMaxOpenConns(5)
}
GORM.SetLogger(&GORMLogger{})
return err
}
func InitSchema(schema string, name string) bool {
_, err := PQ.Exec(schema)
if err != nil {
logrus.WithError(err).Error("failed initializing postgres db schema for ", name)
return false
}
return true
}