forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
127 lines (102 loc) · 2.54 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
package common
import (
"database/sql"
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/jonas747/discordgo"
"github.com/mediocregopher/radix.v2/pool"
"github.com/sirupsen/logrus"
"github.com/volatiletech/sqlboiler/boil"
stdlog "log"
"os"
)
const (
VERSIONMAJOR = 0
VERSIONMINOR = 28
VERSIONPATCH = 1
Testing = false // Disables stuff like command cooldowns
)
var (
VERSIONNUMBER = fmt.Sprintf("%d.%d.%d", VERSIONMAJOR, VERSIONMINOR, VERSIONPATCH)
VERSION = VERSIONNUMBER + " Quadriphonic"
GORM *gorm.DB
PQ *sql.DB
RedisPool *pool.Pool
DSQLStateDB *sql.DB
BotSession *discordgo.Session
BotUser *discordgo.User
Conf *CoreConfig
RedisPoolSize = 25
)
// 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
BotSession, err = discordgo.New(config.BotToken)
if err != nil {
return err
}
BotSession.MaxRestRetries = 3
err = connectRedis(config.Redis)
if err != nil {
return err
}
err = connectDB(config.PQUsername, config.PQPassword, "yagpdb")
BotUser, err = BotSession.UserMe()
if err != nil {
panic(err)
}
return err
}
func InitTest() {
testDB := os.Getenv("YAGPDB_TEST_DB")
if testDB == "" {
return
}
err := connectDB("postgres", "123", testDB)
if err != nil {
panic(err)
}
}
func connectRedis(addr string) (err error) {
// RedisPool, err = pool.NewCustom("tcp", addr, 25, redis.)
RedisPool, err = pool.NewCustom("tcp", addr, RedisPoolSize, RedisDialFunc)
if err != nil {
logrus.WithError(err).Fatal("Failed initilizing redis pool")
}
return
}
func connectDB(user, pass, dbName string) error {
db, err := gorm.Open("postgres", fmt.Sprintf("host=localhost user=%s dbname=%s sslmode=disable password=%s", user, dbName, pass))
GORM = db
PQ = db.DB()
boil.SetDB(PQ)
if err == nil {
PQ.SetMaxOpenConns(5)
}
if os.Getenv("YAGPDB_SQLSTATE_ADDR") != "" {
logrus.Info("Using special sql state db")
addr := os.Getenv("YAGPDB_SQLSTATE_ADDR")
user := os.Getenv("YAGPDB_SQLSTATE_USER")
pass := os.Getenv("YAGPDB_SQLSTATE_PW")
dbName := os.Getenv("YAGPDB_SQLSTATE_DB")
db, err := sql.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable password=%s", addr, user, dbName, pass))
if err != nil {
DSQLStateDB = PQ
return err
}
DSQLStateDB = db
} else {
DSQLStateDB = PQ
}
return err
}