feat: 支持sqlite存储数据

This commit is contained in:
meilin.huang
2023-10-10 23:21:29 +08:00
parent 22e218fc5f
commit 41443dccc0
6 changed files with 72 additions and 16 deletions

View File

@@ -17,6 +17,7 @@ jwt:
# 资源密码aes加密key
aes:
key: 1111111111111111
# 若存在mysql配置优先使用mysql
mysql:
# 自动升级数据库
auto-migration: false
@@ -26,6 +27,9 @@ mysql:
db-name: mayfly-go
config: charset=utf8&loc=Local&parseTime=true
max-idle-conns: 5
sqlite:
path: ./mayfly-go.sqlite
max-idle-conns: 5
# 若同时部署多台机器则需要配置redis信息用于缓存权限码、验证码、公私钥等
# redis:
# host: localhost

View File

@@ -28,6 +28,7 @@ require (
gopkg.in/yaml.v3 v3.0.1
// gorm
gorm.io/driver/mysql v1.5.1
gorm.io/driver/sqlite v1.5.4
gorm.io/gorm v1.25.4
)
@@ -54,6 +55,7 @@ require (
github.com/kr/pretty v0.3.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect

BIN
server/mayfly-go.sqlite Normal file

Binary file not shown.

View File

@@ -52,6 +52,7 @@ type Config struct {
Jwt Jwt `yaml:"jwt"`
Aes Aes `yaml:"aes"`
Mysql Mysql `yaml:"mysql"`
Sqlite Sqlite `yaml:"sqlite"`
Redis Redis `yaml:"redis"`
Log Log `yaml:"log"`
}
@@ -70,6 +71,7 @@ func (c *Config) IfBlankDefaultValue() {
c.Server.Default()
c.Jwt.Default()
c.Mysql.Default()
c.Sqlite.Default()
}
// 配置文件内容校验
@@ -109,6 +111,11 @@ func (c *Config) ReplaceOsEnv() {
c.Mysql.Password = dbPwd
}
sqlitePath := os.Getenv("MAYFLY_SQLITE_PATH")
if sqlitePath != "" {
c.Sqlite.Path = sqlitePath
}
aesKey := os.Getenv("MAYFLY_AES_KEY")
if aesKey != "" {
c.Aes.Key = aesKey

View File

@@ -0,0 +1,22 @@
package config
import "mayfly-go/pkg/logx"
type Sqlite struct {
Path string `mapstructure:"path" json:"path" yaml:"path"`
MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"`
MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"`
}
func (m *Sqlite) Default() {
if m.Path == "" {
m.Path = "./mayfly-go.sqlite"
logx.Warnf("未配置sqlite.path, 默认值: %s", m.Path)
}
if m.MaxIdleConns == 0 {
m.MaxIdleConns = 5
}
if m.MaxOpenConns == 0 {
m.MaxOpenConns = m.MaxIdleConns
}
}

View File

@@ -8,21 +8,27 @@ import (
"time"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
)
func initDb() {
global.Db = gormMysql()
global.Db = initGormDb()
}
func gormMysql() *gorm.DB {
func initGormDb() *gorm.DB {
m := config.Conf.Mysql
if m.Dbname == "" {
logx.Panic("未找到数据库配置信息")
return nil
// 存在msyql数据库名则优先使用mysql
if m.Dbname != "" {
return initMysql(m)
}
return initSqlite(config.Conf.Sqlite)
}
func initMysql(m config.Mysql) *gorm.DB {
logx.Infof("连接mysql [%s]", m.Host)
mysqlConfig := mysql.Config{
DSN: m.Dsn(), // DSN data source name
@@ -33,6 +39,31 @@ func gormMysql() *gorm.DB {
SkipInitializeWithVersion: false, // 根据版本自动配置
}
if db, err := gorm.Open(mysql.New(mysqlConfig), getGormConfig()); err != nil {
logx.Panicf("连接mysql失败! [%s]", err.Error())
return nil
} else {
sqlDB, _ := db.DB()
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
return db
}
}
func initSqlite(sc config.Sqlite) *gorm.DB {
logx.Infof("连接sqlite [%s]", sc.Path)
if db, err := gorm.Open(sqlite.Open(sc.Path), getGormConfig()); err != nil {
logx.Panicf("连接sqlite失败! [%s]", err.Error())
return nil
} else {
sqlDB, _ := db.DB()
sqlDB.SetMaxIdleConns(sc.MaxIdleConns)
sqlDB.SetMaxOpenConns(sc.MaxOpenConns)
return db
}
}
func getGormConfig() *gorm.Config {
sqlLogLevel := logger.Error
logConf := logx.GetConfig()
// 如果为配置文件中配置的系统日志级别为debug则打印gorm执行的sql信息
@@ -50,18 +81,8 @@ func gormMysql() *gorm.DB {
},
)
ormConfig := &gorm.Config{NamingStrategy: schema.NamingStrategy{
return &gorm.Config{NamingStrategy: schema.NamingStrategy{
TablePrefix: "t_",
SingularTable: true,
}, Logger: gormLogger}
if db, err := gorm.Open(mysql.New(mysqlConfig), ormConfig); err != nil {
logx.Panicf("连接mysql失败! [%s]", err.Error())
return nil
} else {
sqlDB, _ := db.DB()
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
return db
}
}