Use mysql as the database
This commit is contained in:
parent
37176b753f
commit
0ce6822978
@ -31,8 +31,17 @@ var (
|
||||
Cron string
|
||||
Notice string
|
||||
Admins []int64
|
||||
Mysql MysqlConfig
|
||||
)
|
||||
|
||||
type MysqlConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
User string
|
||||
Password string
|
||||
DB string
|
||||
}
|
||||
|
||||
func InitConfig() {
|
||||
viper.SetConfigName("config")
|
||||
viper.AddConfigPath(".")
|
||||
@ -56,6 +65,13 @@ func InitConfig() {
|
||||
MaxGoroutines = viper.GetInt("goroutine")
|
||||
Admins = getAdmins()
|
||||
|
||||
Mysql = MysqlConfig{
|
||||
Host: viper.GetString("mysql.host"),
|
||||
Port: viper.GetInt("mysql.port"),
|
||||
User: viper.GetString("mysql.user"),
|
||||
Password: viper.GetString("mysql.password"),
|
||||
DB: viper.GetString("mysql.database"),
|
||||
}
|
||||
viper.WatchConfig()
|
||||
viper.OnConfigChange(func(e fsnotify.Event) {
|
||||
MaxGoroutines = viper.GetInt("goroutine")
|
||||
|
||||
32
model/db.go
Normal file
32
model/db.go
Normal file
@ -0,0 +1,32 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/iyear/E5SubBot/config"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
func InitDB() {
|
||||
var err error
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||
config.Mysql.User,
|
||||
config.Mysql.Password,
|
||||
config.Mysql.Host,
|
||||
config.Mysql.Port,
|
||||
config.Mysql.DB,
|
||||
)
|
||||
DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
NowFunc: func() time.Time {
|
||||
return time.Now()
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
zap.S().Errorw("failed to open db", "error", err)
|
||||
}
|
||||
DB.AutoMigrate(&Client{})
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
func InitDB() {
|
||||
var err error
|
||||
DB, err = gorm.Open(sqlite.Open("data.db"), &gorm.Config{
|
||||
NowFunc: func() time.Time {
|
||||
return time.Now()
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
zap.S().Errorw("failed to open db", "error", err)
|
||||
}
|
||||
DB.AutoMigrate(&Client{})
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user