This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
93 lines (73 loc) · 1.94 KB
/
database.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
package main
import (
"emmApi/models"
"fmt"
"github.com/RediSearch/redisearch-go/redisearch"
goredis "github.com/go-redis/redis/v8"
"github.com/nitishm/go-rejson/v4"
"gorm.io/driver/postgres"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
)
var DatabaseConnection *gorm.DB
var RedisConnection *goredis.Client
var ReJsonClient *rejson.Handler
var RediSearchClient *redisearch.Client
func SetupDatabaseConnection() {
databaseConfig := ServiceConfig.Database
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Etc/UTC",
databaseConfig.Host,
databaseConfig.User,
databaseConfig.Password,
databaseConfig.Database,
databaseConfig.Port)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: gormLogger.Default.LogMode(gormLogger.Silent),
//Logger: gormLogger.Default.LogMode(gormLogger.Info),
})
if err != nil {
panic(err)
}
sqlDb, err := db.DB()
if err != nil {
panic(err)
}
sqlDb.SetMaxIdleConns(databaseConfig.MaxIdleConnections)
sqlDb.SetMaxOpenConns(databaseConfig.MaxOpenConnections)
err = db.AutoMigrate(&models.Avatar{})
if err != nil {
fmt.Println(err)
}
err = db.AutoMigrate(&models.User{})
if err != nil {
fmt.Println(err)
}
err = db.AutoMigrate(&models.BlacklistedAuthor{})
if err != nil {
fmt.Println(err)
}
err = db.AutoMigrate(&models.PersistentToken{})
if err != nil {
fmt.Println(err)
}
err = db.AutoMigrate(&models.Ban{})
if err != nil {
fmt.Println(err)
}
err = db.AutoMigrate(&models.AvatarFavorite{})
if err != nil {
fmt.Println(err)
}
DatabaseConnection = db
}
func SetupRedisConnection() {
redisConfig := ServiceConfig.Redis
host := fmt.Sprintf("%s:%d", redisConfig.Host, redisConfig.Port)
rh := rejson.NewReJSONHandler()
client := goredis.NewClient(&goredis.Options{Addr: host})
rs := redisearch.NewClient(host, "avatarSearch")
rh.SetGoRedisClient(client)
RedisConnection = client
ReJsonClient = rh
RediSearchClient = rs
}