-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.go
167 lines (157 loc) · 4.03 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
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
package database
import (
"fmt"
"github.com/elabosak233/cloudsdale/internal/config"
"github.com/elabosak233/cloudsdale/internal/logger/adapter"
"github.com/elabosak233/cloudsdale/internal/model"
"go.uber.org/zap"
"golang.org/x/crypto/bcrypt"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"time"
)
var db *gorm.DB
var dbInfo string
func InitDatabase() {
initDatabaseEngine()
zap.L().Info(fmt.Sprintf("Database Connect Information: %s", dbInfo))
db.Logger = adapter.NewGORMAdapter(zap.L())
syncDatabase()
initAdmin()
initCategory()
selfCheck()
}
func Db() *gorm.DB {
return db
}
func Debug() {
db = db.Debug()
}
func initDatabaseEngine() {
var err error
switch config.AppCfg().Db.Provider {
case "postgres":
dbInfo = fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
config.AppCfg().Db.Postgres.Host,
config.AppCfg().Db.Postgres.Port,
config.AppCfg().Db.Postgres.Username,
config.AppCfg().Db.Postgres.Password,
config.AppCfg().Db.Postgres.Dbname,
config.AppCfg().Db.Postgres.Sslmode,
)
db, err = gorm.Open(postgres.Open(dbInfo), &gorm.Config{})
case "mysql":
dbInfo = fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
config.AppCfg().Db.MySQL.Username,
config.AppCfg().Db.MySQL.Password,
config.AppCfg().Db.MySQL.Host,
config.AppCfg().Db.MySQL.Port,
config.AppCfg().Db.MySQL.Dbname,
)
db, err = gorm.Open(mysql.Open(dbInfo), &gorm.Config{})
case "sqlite3":
dbInfo = config.AppCfg().Db.SQLite3.Filename
db, err = gorm.Open(sqlite.Open(dbInfo), &gorm.Config{})
}
if err != nil {
zap.L().Fatal("Database connection failed.", zap.Error(err))
}
}
func syncDatabase() {
err := db.AutoMigrate(
&model.User{},
&model.Category{},
&model.Challenge{},
&model.Team{},
&model.UserTeam{},
&model.Submission{},
&model.Nat{},
&model.Hint{},
&model.Pod{},
&model.Game{},
&model.GameChallenge{},
&model.GameTeam{},
&model.Flag{},
&model.FlagGen{},
&model.Port{},
&model.Nat{},
&model.Env{},
&model.Notice{},
)
if err != nil {
zap.L().Fatal("Database sync failed.", zap.Error(err))
}
}
func selfCheck() {
// 对于 pods 中的所有数据,若 removed_at 大于当前时间,则强制赋值为现在的时间,以免后续程序错误判断
db.Model(&model.Pod{}).Where("removed_at > ?", time.Now().UnixMilli()).Update("removed_at", time.Now().UnixMilli())
}
func initAdmin() {
var count int64
db.Model(&model.User{}).Where("username = ?", "admin").Count(&count)
if count == 0 {
zap.L().Warn("Administrator account does not exist, will be created soon.")
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte("123456"), bcrypt.DefaultCost)
admin := model.User{
Username: "admin",
Nickname: "Administrator",
Group: "admin",
Password: string(hashedPassword),
Email: "admin@admin.com",
}
err := db.Create(&admin).Error
if err != nil {
zap.L().Fatal("Super administrator account creation failed.", zap.Error(err))
return
}
zap.L().Info("Super administrator account created successfully.")
}
}
func initCategory() {
var count int64
db.Model(&model.Category{}).Count(&count)
if count == 0 {
zap.L().Warn("Categories do not exist, will be created soon.")
defaultCategories := []model.Category{
{
Name: "misc",
Description: "misc",
Color: "#3F51B5",
Icon: "fingerprint",
},
{
Name: "web",
Description: "web",
Color: "#009688",
Icon: "language",
},
{
Name: "pwn",
Description: "pwn",
Color: "#673AB7",
Icon: "function",
},
{
Name: "crypto",
Description: "crypto",
Color: "#607D8B",
Icon: "tag",
},
{
Name: "reverse",
Description: "reverse",
Color: "#6D4C41",
Icon: "keyboard_double_arrow_left",
},
}
err := db.Create(&defaultCategories).Error
if err != nil {
zap.L().Fatal("Category initialization failed.", zap.Error(err))
return
}
}
}