From 87aa30b231a7d505006639e193be4b4b41e1f1bd Mon Sep 17 00:00:00 2001 From: SliverHorn <503551462@qq.com> Date: Mon, 15 Nov 2021 23:50:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20-=20=E4=BC=98=E5=8C=96=20postgresql?= =?UTF-8?q?=20=E7=9A=84=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=20-=20dsn=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20-=20gorm=20logger=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20-=20=E4=BC=98=E5=8C=96=20=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E8=A1=A8=20=E5=87=BD=E6=95=B0=E5=90=8D=20-=20?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=20go.mod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config/gorm.go | 31 ------ server/config/gorm_mysql.go | 18 ++++ server/config/gorm_pgsql.go | 18 ++++ server/go.mod | 1 + server/initialize/gorm.go | 66 ++++-------- server/initialize/gorm_mysql.go | 27 ++--- server/initialize/gorm_pgsql.go | 18 ++-- server/initialize/internal/gorm.go | 36 +++++++ server/initialize/internal/logger.go | 153 +++------------------------ server/main.go | 2 +- 10 files changed, 127 insertions(+), 243 deletions(-) delete mode 100644 server/config/gorm.go create mode 100644 server/config/gorm_mysql.go create mode 100644 server/config/gorm_pgsql.go create mode 100644 server/initialize/internal/gorm.go diff --git a/server/config/gorm.go b/server/config/gorm.go deleted file mode 100644 index 535d83b0..00000000 --- a/server/config/gorm.go +++ /dev/null @@ -1,31 +0,0 @@ -package config - -type Mysql struct { - Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址 - Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口 - Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置 - Dbname string `mapstructure:"db-name" json:"dbname" yaml:"db-name"` // 数据库名 - Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名 - Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码 - MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"` // 空闲中的最大连接数 - MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"` // 打开到数据库的最大连接数 - LogMode string `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"` // 是否开启Gorm全局日志 - LogZap bool `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"` // 是否通过zap写入日志文件 -} - -type Pgsql struct { - Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址:端口 - Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口 - Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置 - Dbname string `mapstructure:"db-name" json:"dbname" yaml:"db-name"` // 数据库名 - Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名 - Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码 - MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"` // 空闲中的最大连接数 - MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"` // 打开到数据库的最大连接数 - LogMode string `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"` // 是否开启Gorm全局日志 - LogZap bool `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"` // 是否通过zap写入日志文件 -} - -func (m *Mysql) Dsn() string { - return m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config -} diff --git a/server/config/gorm_mysql.go b/server/config/gorm_mysql.go new file mode 100644 index 00000000..b596c29b --- /dev/null +++ b/server/config/gorm_mysql.go @@ -0,0 +1,18 @@ +package config + +type Mysql struct { + Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址 + Port string `mapstructure:"port" json:"port" yaml:"port"` // 端口 + Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置 + Dbname string `mapstructure:"db-name" json:"dbname" yaml:"db-name"` // 数据库名 + Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名 + Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码 + MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"` // 空闲中的最大连接数 + MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"` // 打开到数据库的最大连接数 + LogMode string `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"` // 是否开启Gorm全局日志 + LogZap bool `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"` // 是否通过zap写入日志文件 +} + +func (m *Mysql) Dsn() string { + return m.Username + ":" + m.Password + "@tcp(" + m.Path + ":" + m.Port + ")/" + m.Dbname + "?" + m.Config +} diff --git a/server/config/gorm_pgsql.go b/server/config/gorm_pgsql.go new file mode 100644 index 00000000..40464900 --- /dev/null +++ b/server/config/gorm_pgsql.go @@ -0,0 +1,18 @@ +package config + +type Pgsql struct { + Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址:端口 + Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口 + Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置 + Dbname string `mapstructure:"db-name" json:"dbname" yaml:"db-name"` // 数据库名 + Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名 + Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码 + MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"` // 空闲中的最大连接数 + MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"` // 打开到数据库的最大连接数 + LogMode string `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"` // 是否开启Gorm全局日志 + LogZap bool `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"` // 是否通过zap写入日志文件 +} + +func (p *Pgsql) Dsn() string { + return "host=" + p.Path + " user=" + p.Username + " password=" + p.Password + " dbname=" + p.Dbname + " port=" + p.Port + " " + p.Config +} diff --git a/server/go.mod b/server/go.mod index a166aa3d..33c647e2 100644 --- a/server/go.mod +++ b/server/go.mod @@ -36,6 +36,7 @@ require ( golang.org/x/sync v0.0.0-20210220032951-036812b2e83c gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect gorm.io/driver/mysql v1.0.1 + gorm.io/driver/postgres v0.2.6 gorm.io/gorm v1.20.11 nhooyr.io/websocket v1.8.6 ) diff --git a/server/initialize/gorm.go b/server/initialize/gorm.go index 62694486..2d4741c6 100644 --- a/server/initialize/gorm.go +++ b/server/initialize/gorm.go @@ -1,24 +1,18 @@ package initialize import ( - "github.com/flipped-aurora/gin-vue-admin/server/initialize/internal" - "gorm.io/gorm/logger" - "os" - "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/model/autocode" "github.com/flipped-aurora/gin-vue-admin/server/model/example" "github.com/flipped-aurora/gin-vue-admin/server/model/system" + "os" "go.uber.org/zap" "gorm.io/gorm" ) -//@author: SliverHorn -//@function: Gorm -//@description: 初始化数据库并产生数据库全局变量 -//@return: *gorm.DB - +// Gorm 初始化数据库并产生数据库全局变量 +// Author SliverHorn func Gorm() *gorm.DB { switch global.GVA_CONFIG.System.DbType { case "mysql": @@ -30,58 +24,36 @@ func Gorm() *gorm.DB { } } -// MysqlTables -//@author: SliverHorn -//@function: MysqlTables -//@description: 注册数据库表专用 -//@param: db *gorm.DB - -func MysqlTables(db *gorm.DB) { +// RegisterTables 注册数据库表专用 +// Author SliverHorn +func RegisterTables(db *gorm.DB) { err := db.AutoMigrate( - system.SysUser{}, - system.SysAuthority{}, + // 系统模块表 system.SysApi{}, + system.SysUser{}, system.SysBaseMenu{}, - system.SysBaseMenuParameter{}, system.JwtBlacklist{}, + system.SysAuthority{}, system.SysDictionary{}, + system.SysOperationRecord{}, + system.SysAutoCodeHistory{}, system.SysDictionaryDetail{}, - example.ExaFileUploadAndDownload{}, + system.SysBaseMenuParameter{}, + + // 示例模块表 example.ExaFile{}, - example.ExaFileChunk{}, example.ExaCustomer{}, - system.SysOperationRecord{}, - system.SysAutoCodeHistory{}, + example.ExaFileChunk{}, + example.ExaFileUploadAndDownload{}, + + // 自动化模块表 // Code generated by github.com/flipped-aurora/gin-vue-admin/server Begin; DO NOT EDIT. autocode.AutoCodeExample{}, // Code generated by github.com/flipped-aurora/gin-vue-admin/server End; DO NOT EDIT. ) if err != nil { - global.GVA_LOG.Error("register table failed", zap.Any("err", err)) + global.GVA_LOG.Error("register table failed", zap.Error(err)) os.Exit(0) } global.GVA_LOG.Info("register table success") } - -//@author: SliverHorn -//@function: gormConfig -//@description: 根据配置决定是否开启日志 -//@param: mod bool -//@return: *gorm.Config - -func gormConfig() *gorm.Config { - config := &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true} - switch global.GVA_CONFIG.Mysql.LogMode { - case "silent", "Silent": - config.Logger = internal.Default.LogMode(logger.Silent) - case "error", "Error": - config.Logger = internal.Default.LogMode(logger.Error) - case "warn", "Warn": - config.Logger = internal.Default.LogMode(logger.Warn) - case "info", "Info": - config.Logger = internal.Default.LogMode(logger.Info) - default: - config.Logger = internal.Default.LogMode(logger.Info) - } - return config -} diff --git a/server/initialize/gorm_mysql.go b/server/initialize/gorm_mysql.go index 7f21b4b7..c0baaf8a 100644 --- a/server/initialize/gorm_mysql.go +++ b/server/initialize/gorm_mysql.go @@ -2,33 +2,28 @@ package initialize import ( "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/initialize/internal" "gorm.io/driver/mysql" "gorm.io/gorm" ) -//@author: SliverHorn -//@function: GormMysql -//@description: 初始化Mysql数据库 -//@return: *gorm.DB - +// GormMysql 初始化Mysql数据库 +// Author piexlmax +// Author SliverHorn func GormMysql() *gorm.DB { m := global.GVA_CONFIG.Mysql if m.Dbname == "" { return nil } - dsn := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config mysqlConfig := mysql.Config{ - DSN: dsn, // DSN data source name - DefaultStringSize: 191, // string 类型字段的默认长度 - DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持 - DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引 - DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列 - SkipInitializeWithVersion: false, // 根据版本自动配置 + DSN: m.Dsn(), // DSN data source name + DefaultStringSize: 191, // string 类型字段的默认长度 + DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持 + DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引 + DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列 + SkipInitializeWithVersion: false, // 根据版本自动配置 } - if db, err := gorm.Open(mysql.New(mysqlConfig), gormConfig()); err != nil { - //global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err)) - //os.Exit(0) - //return nil + if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config()); err != nil { return nil } else { sqlDB, _ := db.DB() diff --git a/server/initialize/gorm_pgsql.go b/server/initialize/gorm_pgsql.go index 0d4016b7..b9a3b30b 100644 --- a/server/initialize/gorm_pgsql.go +++ b/server/initialize/gorm_pgsql.go @@ -2,30 +2,24 @@ package initialize import ( "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/initialize/internal" "gorm.io/driver/postgres" "gorm.io/gorm" ) -//@author: SliverHorn -//@function: GormMysql -//@description: 初始化Mysql数据库 -//@return: *gorm.DB - +// GormPgSql 初始化 Postgresql 数据库 +// Author piexlmax +// Author SliverHorn func GormPgSql() *gorm.DB { p := global.GVA_CONFIG.Pgsql if p.Dbname == "" { return nil } - - dsn := "host=" + p.Path + " user=" + p.Username + " password=" + p.Password + " dbname=" + p.Dbname + " port=" + p.Port + " " + p.Config pgsqlConfig := postgres.Config{ - DSN: dsn, // DSN data source name + DSN: p.Dsn(), // DSN data source name PreferSimpleProtocol: false, } - if db, err := gorm.Open(postgres.New(pgsqlConfig), gormConfig()); err != nil { - //global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err)) - //os.Exit(0) - //return nil + if db, err := gorm.Open(postgres.New(pgsqlConfig), internal.Gorm.Config()); err != nil { return nil } else { sqlDB, _ := db.DB() diff --git a/server/initialize/internal/gorm.go b/server/initialize/internal/gorm.go new file mode 100644 index 00000000..3b34a78e --- /dev/null +++ b/server/initialize/internal/gorm.go @@ -0,0 +1,36 @@ +package internal + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "gorm.io/gorm" + "gorm.io/gorm/logger" + "log" + "os" + "time" +) + +var Gorm = new(_gorm) + +type _gorm struct{} + +func (g *_gorm) Config() *gorm.Config { + config := &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true} + _default := logger.New(NewWriter(log.New(os.Stdout, "\r\n", log.LstdFlags)), logger.Config{ + SlowThreshold: 200 * time.Millisecond, + LogLevel: logger.Warn, + Colorful: true, + }) + switch global.GVA_CONFIG.Mysql.LogMode { + case "silent", "Silent": + config.Logger = _default.LogMode(logger.Silent) + case "error", "Error": + config.Logger = _default.LogMode(logger.Error) + case "warn", "Warn": + config.Logger = _default.LogMode(logger.Warn) + case "info", "Info": + config.Logger = _default.LogMode(logger.Info) + default: + config.Logger = _default.LogMode(logger.Info) + } + return config +} diff --git a/server/initialize/internal/logger.go b/server/initialize/internal/logger.go index 25b7b8ca..e04a5a2d 100644 --- a/server/initialize/internal/logger.go +++ b/server/initialize/internal/logger.go @@ -1,153 +1,34 @@ package internal import ( - "context" "fmt" - "io/ioutil" - "log" - "os" - "time" - "github.com/flipped-aurora/gin-vue-admin/server/global" "gorm.io/gorm/logger" - "gorm.io/gorm/utils" ) -type config struct { - SlowThreshold time.Duration - Colorful bool - LogLevel logger.LogLevel -} - -var ( - Discard = New(log.New(ioutil.Discard, "", log.LstdFlags), config{}) - Default = New(log.New(os.Stdout, "\r\n", log.LstdFlags), config{ - SlowThreshold: 200 * time.Millisecond, - LogLevel: logger.Warn, - Colorful: true, - }) - Recorder = traceRecorder{Interface: Default, BeginAt: time.Now()} -) - -func New(writer logger.Writer, config config) logger.Interface { - var ( - infoStr = "%s\n[info] " - warnStr = "%s\n[warn] " - errStr = "%s\n[error] " - traceStr = "%s\n[%.3fms] [rows:%v] %s\n" - traceWarnStr = "%s %s\n[%.3fms] [rows:%v] %s\n" - traceErrStr = "%s %s\n[%.3fms] [rows:%v] %s\n" - ) - - if config.Colorful { - infoStr = logger.Green + "%s\n" + logger.Reset + logger.Green + "[info] " + logger.Reset - warnStr = logger.BlueBold + "%s\n" + logger.Reset + logger.Magenta + "[warn] " + logger.Reset - errStr = logger.Magenta + "%s\n" + logger.Reset + logger.Red + "[error] " + logger.Reset - traceStr = logger.Green + "%s\n" + logger.Reset + logger.Yellow + "[%.3fms] " + logger.BlueBold + "[rows:%v]" + logger.Reset + " %s\n" - traceWarnStr = logger.Green + "%s " + logger.Yellow + "%s\n" + logger.Reset + logger.RedBold + "[%.3fms] " + logger.Yellow + "[rows:%v]" + logger.Magenta + " %s\n" + logger.Reset - traceErrStr = logger.RedBold + "%s " + logger.MagentaBold + "%s\n" + logger.Reset + logger.Yellow + "[%.3fms] " + logger.BlueBold + "[rows:%v]" + logger.Reset + " %s\n" - } - - return &_logger{ - Writer: writer, - config: config, - infoStr: infoStr, - warnStr: warnStr, - errStr: errStr, - traceStr: traceStr, - traceWarnStr: traceWarnStr, - traceErrStr: traceErrStr, - } -} - -type _logger struct { - config +type writer struct { logger.Writer - infoStr, warnStr, errStr string - traceStr, traceErrStr, traceWarnStr string -} - -// LogMode log mode -func (c *_logger) LogMode(level logger.LogLevel) logger.Interface { - newLogger := *c - newLogger.LogLevel = level - return &newLogger -} - -// Info print info -func (c *_logger) Info(ctx context.Context, message string, data ...interface{}) { - if c.LogLevel >= logger.Info { - c.Printf(c.infoStr+message, append([]interface{}{utils.FileWithLineNum()}, data...)...) - } } -// Warn print warn messages -func (c *_logger) Warn(ctx context.Context, message string, data ...interface{}) { - if c.LogLevel >= logger.Warn { - c.Printf(c.warnStr+message, append([]interface{}{utils.FileWithLineNum()}, data...)...) - } +// NewWriter writer 构造函数 +// Author SliverHorn +func NewWriter(w logger.Writer) *writer { + return &writer{Writer: w} } -// Error print error messages -func (c *_logger) Error(ctx context.Context, message string, data ...interface{}) { - if c.LogLevel >= logger.Error { - c.Printf(c.errStr+message, append([]interface{}{utils.FileWithLineNum()}, data...)...) +// Printf 格式化打印日志 +// Author SliverHorn +func (w *writer) Printf(message string, data ...interface{}) { + var logZap bool + switch global.GVA_CONFIG.System.DbType { + case "mysql": + logZap = global.GVA_CONFIG.Mysql.LogZap + case "pgsql": + logZap = global.GVA_CONFIG.Pgsql.LogZap } -} - -// Trace print sql message -func (c *_logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { - if c.LogLevel > 0 { - elapsed := time.Since(begin) - switch { - case err != nil && c.LogLevel >= logger.Error: - sql, rows := fc() - if rows == -1 { - c.Printf(c.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql) - } else { - c.Printf(c.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, rows, sql) - } - case elapsed > c.SlowThreshold && c.SlowThreshold != 0 && c.LogLevel >= logger.Warn: - sql, rows := fc() - slowLog := fmt.Sprintf("SLOW SQL >= %v", c.SlowThreshold) - if rows == -1 { - c.Printf(c.traceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, "-", sql) - } else { - c.Printf(c.traceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, rows, sql) - } - case c.LogLevel >= logger.Info: - sql, rows := fc() - if rows == -1 { - c.Printf(c.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, "-", sql) - } else { - c.Printf(c.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql) - } - } - } -} - -func (c *_logger) Printf(message string, data ...interface{}) { - if global.GVA_CONFIG.Mysql.LogZap { - global.GVA_LOG.Info(fmt.Sprintf(message, data...)) + if logZap { + global.GVA_LOG.Info(fmt.Sprintf(message+"\n", data...)) } else { - c.Writer.Printf(message, data...) + w.Writer.Printf(message, data...) } } - -type traceRecorder struct { - logger.Interface - BeginAt time.Time - SQL string - RowsAffected int64 - Err error -} - -func (t traceRecorder) New() *traceRecorder { - return &traceRecorder{Interface: t.Interface, BeginAt: time.Now()} -} - -func (t *traceRecorder) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { - t.BeginAt = begin - t.SQL, t.RowsAffected = fc() - t.Err = err -} diff --git a/server/main.go b/server/main.go index c82e084b..e607749a 100644 --- a/server/main.go +++ b/server/main.go @@ -24,7 +24,7 @@ func main() { global.GVA_DB = initialize.Gorm() // gorm连接数据库 initialize.Timer() if global.GVA_DB != nil { - initialize.MysqlTables(global.GVA_DB) // 初始化表 + initialize.RegisterTables(global.GVA_DB) // 初始化表 // 程序结束前关闭数据库链接 db, _ := global.GVA_DB.DB() defer db.Close()