mirror of
https://github.com/cloudreve/cloudreve.git
synced 2025-10-29 23:55:51 +08:00
Test: conf
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"Cloudreve/pkg/conf"
|
||||
"Cloudreve/pkg/util"
|
||||
"fmt"
|
||||
"github.com/jinzhu/gorm"
|
||||
"time"
|
||||
|
||||
@ -15,13 +17,32 @@ var DB *gorm.DB
|
||||
func Init() {
|
||||
//TODO 从配置文件中读取 包括DEBUG模式
|
||||
util.Log().Info("初始化数据库连接\n")
|
||||
db, err := gorm.Open("mysql", "root:root@(localhost)/v3?charset=utf8&parseTime=True&loc=Local")
|
||||
|
||||
var (
|
||||
db *gorm.DB
|
||||
err error
|
||||
)
|
||||
if conf.DatabaseConfig.Type == "UNSET" {
|
||||
//TODO 使用内置SQLite数据库
|
||||
} else {
|
||||
db, err = gorm.Open(conf.DatabaseConfig.Type, fmt.Sprintf("%s:%s@(%s)/%s?charset=utf8&parseTime=True&loc=Local",
|
||||
conf.DatabaseConfig.User,
|
||||
conf.DatabaseConfig.Password,
|
||||
conf.DatabaseConfig.Host,
|
||||
conf.DatabaseConfig.Name))
|
||||
}
|
||||
|
||||
// 处理表前缀
|
||||
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
|
||||
return conf.DatabaseConfig.TablePrefix + defaultTableName
|
||||
}
|
||||
|
||||
db.LogMode(true)
|
||||
//db.SetLogger(util.Log())
|
||||
// Error
|
||||
if err != nil {
|
||||
util.Log().Panic("连接数据库不成功", err)
|
||||
}
|
||||
|
||||
//设置连接池
|
||||
//空闲
|
||||
db.DB().SetMaxIdleConns(50)
|
||||
@ -32,5 +53,6 @@ func Init() {
|
||||
|
||||
DB = db
|
||||
|
||||
//执行迁移
|
||||
migration()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user