From 704e6db4be0e33bc15eec1da0f8d80d3e9342a59 Mon Sep 17 00:00:00 2001 From: Blizzard Date: Sat, 26 Apr 2025 00:02:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BF=A1=E6=81=AF:=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=EF=BC=8CdbType?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 11 +++++---- config/db_list.go | 52 +++++++++++++++++++++++++++++++++++++++++++ config/gorm_mysql.go | 14 +----------- config/gorm_pgsql.go | 18 +++------------ config/gorm_sqlite.go | 13 +++++++++++ config/system.go | 6 +++++ 6 files changed, 82 insertions(+), 32 deletions(-) create mode 100644 config/db_list.go create mode 100644 config/gorm_sqlite.go create mode 100644 config/system.go diff --git a/config/config.go b/config/config.go index b6f9437..759b882 100644 --- a/config/config.go +++ b/config/config.go @@ -1,7 +1,10 @@ package config -type Server struct { - Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"` - Redis Redis `mapstructure:"redis" json:"redis" yaml:"redis"` - Zap Zap `mapstructure:"zap" json:"zap" yaml:"zap"` +type Config struct { + System System `mapstructure:"system" json:"system" yaml:"system"` + Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"` + Pgsql Pgsql `mapstructure:"pgsql" json:"pgsql" yaml:"pgsql"` + Sqlite Sqlite `mapstructure:"sqlite" json:"sqlite" yaml:"sqlite"` + Redis Redis `mapstructure:"redis" json:"redis" yaml:"redis"` + Zap Zap `mapstructure:"zap" json:"zap" yaml:"zap"` } diff --git a/config/db_list.go b/config/db_list.go new file mode 100644 index 0000000..5c9edf4 --- /dev/null +++ b/config/db_list.go @@ -0,0 +1,52 @@ +package config + +import ( + "gorm.io/gorm/logger" + "strings" +) + +type DsnProvider interface { + Dsn() string +} + +// Embeded 结构体可以压平到上一层,从而保持 config 文件的结构和原来一样 +// 见 playground: https://go.dev/play/p/KIcuhqEoxmY + +// GeneralDB 也被 Pgsql 和 Mysql 原样使用 +type GeneralDB struct { + Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"` // 数据库前缀 + Port string `mapstructure:"port" json:"port" yaml:"port"` // 数据库端口 + Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置 + Dbname string `mapstructure:"db-name" json:"db-name" yaml:"db-name"` // 数据库名 + User string `mapstructure:"user" json:"user" yaml:"user"` // 数据库账号 + Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码 + Host string `mapstructure:"host" json:"host" yaml:"host"` // 数据库地址 + Engine string `mapstructure:"engine" json:"engine" yaml:"engine" default:"InnoDB"` // 数据库引擎,默认InnoDB + LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志 + MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数 + MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数 + Singular bool `mapstructure:"singular" json:"singular" yaml:"singular"` // 是否开启全局禁用复数,true表示开启 + LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` // 是否通过zap写入日志文件 +} + +func (c GeneralDB) LogLevel() logger.LogLevel { + switch strings.ToLower(c.LogMode) { + case "silent", "Silent": + return logger.Silent + case "error", "Error": + return logger.Error + case "warn", "Warn": + return logger.Warn + case "info", "Info": + return logger.Info + default: + return logger.Info + } +} + +type SpecializedDB struct { + Type string `mapstructure:"type" json:"type" yaml:"type"` + AliasName string `mapstructure:"alias-name" json:"alias-name" yaml:"alias-name"` + GeneralDB `yaml:",inline" mapstructure:",squash"` + Disable bool `mapstructure:"disable" json:"disable" yaml:"disable"` +} diff --git a/config/gorm_mysql.go b/config/gorm_mysql.go index 601cf41..314c342 100644 --- a/config/gorm_mysql.go +++ b/config/gorm_mysql.go @@ -1,19 +1,7 @@ package config type Mysql struct { - Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"` // 数据库前缀 - Port string `mapstructure:"port" json:"port" yaml:"port"` // 数据库端口 - Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置 - Dbname string `mapstructure:"db-name" json:"db-name" yaml:"db-name"` // 数据库名 - User string `mapstructure:"user" json:"user" yaml:"user"` // 数据库账号 - Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码 - Host string `mapstructure:"host" json:"host" yaml:"host"` // 数据库地址 - Engine string `mapstructure:"engine" json:"engine" yaml:"engine" default:"InnoDB"` // 数据库引擎,默认InnoDB - LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志 - MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数 - MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数 - Singular bool `mapstructure:"singular" json:"singular" yaml:"singular"` // 是否开启全局禁用复数,true表示开启 - LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` // 是否通过zap写入日志文件 + GeneralDB `yaml:",inline" mapstructure:",squash"` } func (m *Mysql) Dsn() string { diff --git a/config/gorm_pgsql.go b/config/gorm_pgsql.go index 8767cd0..acfbde4 100644 --- a/config/gorm_pgsql.go +++ b/config/gorm_pgsql.go @@ -1,29 +1,17 @@ package config type Pgsql struct { - Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"` // 数据库前缀 - Port string `mapstructure:"port" json:"port" yaml:"port"` // 数据库端口 - Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置 - Dbname string `mapstructure:"db-name" json:"db-name" yaml:"db-name"` // 数据库名 - User string `mapstructure:"user" json:"user" yaml:"user"` // 数据库账号 - Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码 - Path string `mapstructure:"path" json:"path" yaml:"path"` // 数据库地址 - Engine string `mapstructure:"engine" json:"engine" yaml:"engine" default:"InnoDB"` // 数据库引擎,默认InnoDB - LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志 - MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数 - MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数 - Singular bool `mapstructure:"singular" json:"singular" yaml:"singular"` // 是否开启全局禁用复数,true表示开启 - LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` // 是否通过zap写入日志文件 + GeneralDB `yaml:",inline" mapstructure:",squash"` } // Dsn 基于配置文件获取 dsn // Author [SliverHorn](https://github.com/SliverHorn) func (p *Pgsql) Dsn() string { - return "host=" + p.Path + " user=" + p.User + " password=" + p.Password + " dbname=" + p.Dbname + " port=" + p.Port + " " + p.Config + return "host=" + p.Host + " user=" + p.User + " password=" + p.Password + " dbname=" + p.Dbname + " port=" + p.Port + " " + p.Config } // LinkDsn 根据 dbname 生成 dsn // Author [SliverHorn](https://github.com/SliverHorn) func (p *Pgsql) LinkDsn(dbname string) string { - return "host=" + p.Path + " user=" + p.User + " password=" + p.Password + " dbname=" + dbname + " port=" + p.Port + " " + p.Config + return "host=" + p.Host + " user=" + p.User + " password=" + p.Password + " dbname=" + dbname + " port=" + p.Port + " " + p.Config } diff --git a/config/gorm_sqlite.go b/config/gorm_sqlite.go new file mode 100644 index 0000000..d585670 --- /dev/null +++ b/config/gorm_sqlite.go @@ -0,0 +1,13 @@ +package config + +import ( + "path/filepath" +) + +type Sqlite struct { + GeneralDB `yaml:",inline" mapstructure:",squash"` +} + +func (s *Sqlite) Dsn() string { + return filepath.Join(s.Host, s.Dbname+".db") +} diff --git a/config/system.go b/config/system.go new file mode 100644 index 0000000..1e3ffc5 --- /dev/null +++ b/config/system.go @@ -0,0 +1,6 @@ +package config + +type System struct { + Port int `mapstructure:"port" json:"port" yaml:"port"` + DbType string `mapstructure:"db-type" json:"db-type" yaml:"db-type"` +}