diff --git a/server/config.yml.example b/server/config.yml.example index 88106c0b..14521f0a 100644 --- a/server/config.yml.example +++ b/server/config.yml.example @@ -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 diff --git a/server/go.mod b/server/go.mod index 5a07a412..348292e5 100644 --- a/server/go.mod +++ b/server/go.mod @@ -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 diff --git a/server/mayfly-go.sqlite b/server/mayfly-go.sqlite new file mode 100644 index 00000000..0d09b2e3 Binary files /dev/null and b/server/mayfly-go.sqlite differ diff --git a/server/pkg/config/config.go b/server/pkg/config/config.go index 809b76f0..0db64c79 100644 --- a/server/pkg/config/config.go +++ b/server/pkg/config/config.go @@ -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 diff --git a/server/pkg/config/sqlite.go b/server/pkg/config/sqlite.go new file mode 100644 index 00000000..cfaee761 --- /dev/null +++ b/server/pkg/config/sqlite.go @@ -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 + } +} diff --git a/server/pkg/starter/gorm.go b/server/pkg/starter/gorm.go index 99e1cc31..dde763ac 100644 --- a/server/pkg/starter/gorm.go +++ b/server/pkg/starter/gorm.go @@ -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 - } }