Skip to content

Commit

Permalink
Use mysql as the database
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Jul 6, 2021
1 parent 37176b7 commit 0ce6822
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
16 changes: 16 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(".")
Expand All @@ -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")
Expand Down
32 changes: 32 additions & 0 deletions model/db.go
Original file line number Diff line number Diff line change
@@ -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{})
}
23 changes: 0 additions & 23 deletions model/sqlite.go

This file was deleted.

0 comments on commit 0ce6822

Please sign in to comment.