config(database): 更改默认数据库配置为SQLite

- 引入gfile包用于文件路径操作
- 将默认数据库类型从mysql更改为sqlite
- 设置默认数据库连接字符串为本地db/database.db文件路径
- 修改初始化逻辑以使用配置的dns值
- 更新mysql和sqlite初始化函数使用统一的dns变量
main v1.0.0.00005
black1552 2026-02-03 13:57:39 +08:00
parent a4ca7a754f
commit 8dde22e47c
2 changed files with 7 additions and 6 deletions

View File

@ -57,8 +57,8 @@ func init() {
func SetDefault() {
viper.Set("SERVER.addr", "127.0.0.1:8080")
viper.Set("SERVER.mode", "release")
viper.Set("DATABASE.type", "mysql")
viper.Set("DATABASE.dns", "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local")
viper.Set("DATABASE.type", "sqlite")
viper.Set("DATABASE.dns", gfile.Join(gfile.Pwd(), "db", "database.db"))
viper.Set("JWT.secret", "SET-YOUR-SECRET")
viper.Set("JWT.expire", 86400)
}

View File

@ -6,6 +6,7 @@ import (
"git.magicany.cc/black1552/gin-base/config"
"git.magicany.cc/black1552/gin-base/log"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gfile"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
@ -16,7 +17,7 @@ var (
Db *gorm.DB
err error
sqlDb *sql.DB
dns = config.GetConfigValue("database.dns", "")
dns = config.GetConfigValue("database.dns", gfile.Join(gfile.Pwd(), "db", "database.db"))
)
func init() {
@ -24,7 +25,7 @@ func init() {
log.Error("gormDns未配置", "请检查配置文件")
return
}
switch config.GetConfigValue("database.type", "mysql").String() {
switch config.GetConfigValue("database.type", "sqlite").String() {
case "mysql":
log.Info("使用mysql数据库")
mysqlInit()
@ -50,7 +51,7 @@ func init() {
func mysqlInit() {
Type = mysql.New(mysql.Config{
DSN: config.GetConfigValue("database.dns", "").String(),
DSN: dns.String(),
DefaultStringSize: 255, // string 类型字段的默认长度
DisableDatetimePrecision: true, // 禁用 datetime 精度MySQL 5.6 之前的数据库不支持
DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引
@ -59,5 +60,5 @@ func mysqlInit() {
}
func sqliteInit() {
Type = sqlite.Open(config.GetConfigValue("database.dns", "").String())
Type = sqlite.Open(dns.String())
}