From 95bdd2419ec84c808dd3acb9123d1f336586bbe1 Mon Sep 17 00:00:00 2001 From: SliverHorn <503551462@qq.com> Date: Mon, 29 Nov 2021 21:56:01 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feature:=20=E9=9B=86=E6=88=90=E5=8D=8E?= =?UTF-8?q?=E4=B8=BA=E4=BA=91obs=20=E5=AF=B9=E8=B1=A1=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config.docker.yaml | 10 +++- server/config.yaml | 10 +++- server/config/config.go | 6 +- server/config/oss.go | 32 ----------- server/config/oss_aliyun.go | 10 ++++ server/config/oss_huawei.go | 9 +++ server/config/oss_local.go | 5 ++ server/config/oss_qiniu.go | 11 ++++ server/config/oss_tencent.go | 10 ++++ server/go.mod | 1 + server/go.sum | 2 + server/utils/upload/obs.go | 65 ++++++++++++++++++++++ server/utils/upload/upload.go | 20 +++---- web/src/view/systemTools/system/system.vue | 20 +++++++ 14 files changed, 161 insertions(+), 50 deletions(-) delete mode 100644 server/config/oss.go create mode 100644 server/config/oss_aliyun.go create mode 100644 server/config/oss_huawei.go create mode 100644 server/config/oss_local.go create mode 100644 server/config/oss_qiniu.go create mode 100644 server/config/oss_tencent.go create mode 100644 server/utils/upload/obs.go diff --git a/server/config.docker.yaml b/server/config.docker.yaml index 4ae96ec2..04b79b77 100644 --- a/server/config.docker.yaml +++ b/server/config.docker.yaml @@ -94,7 +94,6 @@ qiniu: secret-key: '' use-cdn-domains: false - # aliyun oss configuration aliyun-oss: endpoint: 'yourEndpoint' @@ -112,11 +111,18 @@ tencent-cos: base-url: 'https://gin.vue.admin' path-prefix: 'github.com/flipped-aurora/gin-vue-admin/server' +# huawei obs configuration +hua-wei-obs: + path: 'you-path' + bucket: 'you-bucket' + endpoint: 'you-endpoint' + access-key: 'you-access-key' + secret-key: 'you-secret-key' + # excel configuration excel: dir: './resource/excel/' - # timer task db clear table Timer: start: true diff --git a/server/config.yaml b/server/config.yaml index fa93b4bf..b94bdd06 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -115,7 +115,6 @@ qiniu: secret-key: '' use-cdn-domains: false - # aliyun oss configuration aliyun-oss: endpoint: 'yourEndpoint' @@ -134,11 +133,18 @@ tencent-cos: base-url: 'https://gin.vue.admin' path-prefix: 'github.com/flipped-aurora/gin-vue-admin/server' +# huawei obs configuration +hua-wei-obs: + path: 'you-path' + bucket: 'you-bucket' + endpoint: 'you-endpoint' + access-key: 'you-access-key' + secret-key: 'you-secret-key' + # excel configuration excel: dir: './resource/excel/' - # timer task db clear table Timer: start: true diff --git a/server/config/config.go b/server/config/config.go index b3267cce..cbf017cc 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -17,7 +17,9 @@ type Server struct { Local Local `mapstructure:"local" json:"local" yaml:"local"` Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"` AliyunOSS AliyunOSS `mapstructure:"aliyun-oss" json:"aliyunOSS" yaml:"aliyun-oss"` + HuaWeiObs HuaWeiObs `mapstructure:"hua-wei-obs" json:"huaWeiObs" yaml:"hua-wei-obs"` TencentCOS TencentCOS `mapstructure:"tencent-cos" json:"tencentCOS" yaml:"tencent-cos"` - Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"` - Timer Timer `mapstructure:"timer" json:"timer" yaml:"timer"` + + Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"` + Timer Timer `mapstructure:"timer" json:"timer" yaml:"timer"` } diff --git a/server/config/oss.go b/server/config/oss.go deleted file mode 100644 index da657586..00000000 --- a/server/config/oss.go +++ /dev/null @@ -1,32 +0,0 @@ -package config - -type Local struct { - Path string `mapstructure:"path" json:"path" yaml:"path"` // 本地文件路径 -} - -type Qiniu struct { - Zone string `mapstructure:"zone" json:"zone" yaml:"zone"` // 存储区域 - Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"` // 空间名称 - ImgPath string `mapstructure:"img-path" json:"imgPath" yaml:"img-path"` // CDN加速域名 - UseHTTPS bool `mapstructure:"use-https" json:"useHttps" yaml:"use-https"` // 是否使用https - AccessKey string `mapstructure:"access-key" json:"accessKey" yaml:"access-key"` // 秘钥AK - SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"` // 秘钥SK - UseCdnDomains bool `mapstructure:"use-cdn-domains" json:"useCdnDomains" yaml:"use-cdn-domains"` // 上传是否使用CDN上传加速 -} - -type AliyunOSS struct { - Endpoint string `mapstructure:"endpoint" json:"endpoint" yaml:"endpoint"` - AccessKeyId string `mapstructure:"access-key-id" json:"accessKeyId" yaml:"access-key-id"` - AccessKeySecret string `mapstructure:"access-key-secret" json:"accessKeySecret" yaml:"access-key-secret"` - BucketName string `mapstructure:"bucket-name" json:"bucketName" yaml:"bucket-name"` - BucketUrl string `mapstructure:"bucket-url" json:"bucketUrl" yaml:"bucket-url"` - BasePath string `mapstructure:"base-path" json:"basePath" yaml:"base-path"` -} -type TencentCOS struct { - Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"` - Region string `mapstructure:"region" json:"region" yaml:"region"` - SecretID string `mapstructure:"secret-id" json:"secretID" yaml:"secret-id"` - SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"` - BaseURL string `mapstructure:"base-url" json:"baseURL" yaml:"base-url"` - PathPrefix string `mapstructure:"path-prefix" json:"pathPrefix" yaml:"path-prefix"` -} diff --git a/server/config/oss_aliyun.go b/server/config/oss_aliyun.go new file mode 100644 index 00000000..519f89fc --- /dev/null +++ b/server/config/oss_aliyun.go @@ -0,0 +1,10 @@ +package config + +type AliyunOSS struct { + Endpoint string `mapstructure:"endpoint" json:"endpoint" yaml:"endpoint"` + AccessKeyId string `mapstructure:"access-key-id" json:"accessKeyId" yaml:"access-key-id"` + AccessKeySecret string `mapstructure:"access-key-secret" json:"accessKeySecret" yaml:"access-key-secret"` + BucketName string `mapstructure:"bucket-name" json:"bucketName" yaml:"bucket-name"` + BucketUrl string `mapstructure:"bucket-url" json:"bucketUrl" yaml:"bucket-url"` + BasePath string `mapstructure:"base-path" json:"basePath" yaml:"base-path"` +} diff --git a/server/config/oss_huawei.go b/server/config/oss_huawei.go new file mode 100644 index 00000000..9462c025 --- /dev/null +++ b/server/config/oss_huawei.go @@ -0,0 +1,9 @@ +package config + +type HuaWeiObs struct { + Path string `mapstructure:"path" json:"path" yaml:"path"` + Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"` + Endpoint string `mapstructure:"endpoint" json:"endpoint" yaml:"endpoint"` + AccessKey string `mapstructure:"access-key" json:"accessKey" yaml:"access-key"` + SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"` +} diff --git a/server/config/oss_local.go b/server/config/oss_local.go new file mode 100644 index 00000000..5ff1f324 --- /dev/null +++ b/server/config/oss_local.go @@ -0,0 +1,5 @@ +package config + +type Local struct { + Path string `mapstructure:"path" json:"path" yaml:"path"` // 本地文件路径 +} diff --git a/server/config/oss_qiniu.go b/server/config/oss_qiniu.go new file mode 100644 index 00000000..af554b78 --- /dev/null +++ b/server/config/oss_qiniu.go @@ -0,0 +1,11 @@ +package config + +type Qiniu struct { + Zone string `mapstructure:"zone" json:"zone" yaml:"zone"` // 存储区域 + Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"` // 空间名称 + ImgPath string `mapstructure:"img-path" json:"imgPath" yaml:"img-path"` // CDN加速域名 + UseHTTPS bool `mapstructure:"use-https" json:"useHttps" yaml:"use-https"` // 是否使用https + AccessKey string `mapstructure:"access-key" json:"accessKey" yaml:"access-key"` // 秘钥AK + SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"` // 秘钥SK + UseCdnDomains bool `mapstructure:"use-cdn-domains" json:"useCdnDomains" yaml:"use-cdn-domains"` // 上传是否使用CDN上传加速 +} diff --git a/server/config/oss_tencent.go b/server/config/oss_tencent.go new file mode 100644 index 00000000..0a7e190b --- /dev/null +++ b/server/config/oss_tencent.go @@ -0,0 +1,10 @@ +package config + +type TencentCOS struct { + Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"` + Region string `mapstructure:"region" json:"region" yaml:"region"` + SecretID string `mapstructure:"secret-id" json:"secretID" yaml:"secret-id"` + SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"` + BaseURL string `mapstructure:"base-url" json:"baseURL" yaml:"base-url"` + PathPrefix string `mapstructure:"path-prefix" json:"pathPrefix" yaml:"path-prefix"` +} diff --git a/server/go.mod b/server/go.mod index 5a52a65e..02feb483 100644 --- a/server/go.mod +++ b/server/go.mod @@ -16,6 +16,7 @@ require ( github.com/go-redis/redis/v8 v8.11.0 github.com/go-sql-driver/mysql v1.5.0 github.com/gookit/color v1.3.1 + github.com/huaweicloud/huaweicloud-sdk-go-obs v3.21.8+incompatible github.com/jordan-wright/email v0.0.0-20200824153738-3f5bafa1cd84 github.com/mojocn/base64Captcha v1.3.1 github.com/natefinch/lumberjack v2.0.0+incompatible diff --git a/server/go.sum b/server/go.sum index 7e9a9f43..00f68f1f 100644 --- a/server/go.sum +++ b/server/go.sum @@ -234,6 +234,8 @@ github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0m github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/huaweicloud/huaweicloud-sdk-go-obs v3.21.8+incompatible h1:3kDd8PIWAdU+qGs/+0QUgsMI2ZSiJPt45Xn0su+x/Q0= +github.com/huaweicloud/huaweicloud-sdk-go-obs v3.21.8+incompatible/go.mod h1:l7VUhRbTKCzdOacdT4oWCwATKyvZqUOlOqr0Ous3k4s= github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0= github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= diff --git a/server/utils/upload/obs.go b/server/utils/upload/obs.go new file mode 100644 index 00000000..2b4efad2 --- /dev/null +++ b/server/utils/upload/obs.go @@ -0,0 +1,65 @@ +package upload + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs" + "github.com/pkg/errors" + "mime/multipart" +) + +var HuaWeiObs = new(_obs) + +type _obs struct{} + +func NewHuaWeiObsClient() (client *obs.ObsClient, err error) { + return obs.New(global.GVA_CONFIG.HuaWeiObs.AccessKey, global.GVA_CONFIG.HuaWeiObs.SecretKey, global.GVA_CONFIG.HuaWeiObs.Endpoint) +} + +func (o *_obs) UploadFile(file *multipart.FileHeader) (filename string, filepath string, err error) { + var open multipart.File + open, err = file.Open() + if err != nil { + return filename, filepath, err + } + filename = file.Filename + input := &obs.PutObjectInput{ + PutObjectBasicInput: obs.PutObjectBasicInput{ + ObjectOperationInput: obs.ObjectOperationInput{ + Bucket: global.GVA_CONFIG.HuaWeiObs.Bucket, + Key: filename, + }, + ContentType: file.Header.Get("content-type"), + }, + Body: open, + } + + var client *obs.ObsClient + client, err = NewHuaWeiObsClient() + if err != nil { + return filepath, filename, errors.Wrap(err, "获取华为对象存储对象失败!") + } + + _, err = client.PutObject(input) + if err != nil { + return filepath, filename, errors.Wrap(err, "文件上传失败!") + } + filepath = global.GVA_CONFIG.HuaWeiObs.Path + "/" + filename + return filepath, filename, err +} + +func (o *_obs) DeleteFile(key string) error { + client, err := NewHuaWeiObsClient() + if err != nil { + return errors.Wrap(err, "获取华为对象存储对象失败!") + } + input := &obs.DeleteObjectInput{ + Bucket: global.GVA_CONFIG.HuaWeiObs.Bucket, + Key: key, + } + var output *obs.DeleteObjectOutput + output, err = client.DeleteObject(input) + if err != nil { + return errors.Wrapf(err, "删除对象(%s)失败!, output: %v", key, output) + } + return nil +} diff --git a/server/utils/upload/upload.go b/server/utils/upload/upload.go index d8015220..f36f5a0f 100644 --- a/server/utils/upload/upload.go +++ b/server/utils/upload/upload.go @@ -6,23 +6,17 @@ import ( "github.com/flipped-aurora/gin-vue-admin/server/global" ) -//@author: [ccfish86](https://github.com/ccfish86) -//@author: [SliverHorn](https://github.com/SliverHorn) -//@interface_name: OSS -//@description: OSS接口 - +// OSS 对象存储接口 +// Author [SliverHorn](https://github.com/SliverHorn) +// Author [ccfish86](https://github.com/ccfish86) type OSS interface { UploadFile(file *multipart.FileHeader) (string, string, error) DeleteFile(key string) error } -//@author: [ccfish86](https://github.com/ccfish86) -//@author: [SliverHorn](https://github.com/SliverHorn) -//@function: NewOss -//@description: OSS接口 -//@description: OSS的实例化方法 -//@return: OSS - +// NewOss OSS的实例化方法 +// Author [SliverHorn](https://github.com/SliverHorn) +// Author [ccfish86](https://github.com/ccfish86) func NewOss() OSS { switch global.GVA_CONFIG.System.OssType { case "local": @@ -33,6 +27,8 @@ func NewOss() OSS { return &TencentCOS{} case "aliyun-oss": return &AliyunOSS{} + case "huawei-obs": + return HuaWeiObs default: return &Local{} } diff --git a/web/src/view/systemTools/system/system.vue b/web/src/view/systemTools/system/system.vue index e11e482b..bd0e2e44 100644 --- a/web/src/view/systemTools/system/system.vue +++ b/web/src/view/systemTools/system/system.vue @@ -22,6 +22,7 @@ + @@ -258,6 +259,24 @@ + @@ -352,6 +371,7 @@ export default { qiniu: {}, tencentCOS: {}, aliyunOSS: {}, + huaWeiObs: {}, captcha: {}, zap: {}, local: {}, From e3df65f354ecfd82d1292c117bbd83060f4b0049 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Tue, 7 Dec 2021 18:46:44 +0800 Subject: [PATCH 2/5] feat: issues #809 --- server/config.yaml | 20 ++++++++++++++++++++ server/config/config.go | 5 +++-- server/config/db_list.go | 21 +++++++++++++++++++++ server/global/global.go | 22 ++++++++++++++++++++++ server/initialize/db_list.go | 24 ++++++++++++++++++++++++ server/initialize/gorm_mysql.go | 21 +++++++++++++++++++++ server/initialize/gorm_pgsql.go | 20 ++++++++++++++++++++ server/main.go | 1 + 8 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 server/config/db_list.go create mode 100644 server/initialize/db_list.go diff --git a/server/config.yaml b/server/config.yaml index b94bdd06..1946fdb8 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -83,6 +83,26 @@ pgsql: log-mode: "" log-zap: false +db-list: [ + { + disabled: true, # 是否启用 + type: "", # 数据库的类型,目前支持mysql、pgsql + alias-name: "", # 数据库的名称,注意: alias-name 需要在db-list中唯一 + path: '', + port: '', + config: '', + db-name: '', + username: '', + password: '', + max-idle-conns: 10, + max-open-conns: 100, + log-mode: "", + log-zap: false, + } +] + + + # local configuration local: path: 'uploads/file' diff --git a/server/config/config.go b/server/config/config.go index cbf017cc..5668edc8 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -11,8 +11,9 @@ type Server struct { // auto AutoCode Autocode `mapstructure:"autoCode" json:"autoCode" yaml:"autoCode"` // gorm - Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"` - Pgsql Pgsql `mapstructure:"pgsql" json:"pgsql" yaml:"pgsql"` + Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"` + Pgsql Pgsql `mapstructure:"pgsql" json:"pgsql" yaml:"pgsql"` + DBList []DB `mapstructure:"db-list" json:"db-list" yaml:"db-list"` // oss Local Local `mapstructure:"local" json:"local" yaml:"local"` Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"` diff --git a/server/config/db_list.go b/server/config/db_list.go new file mode 100644 index 00000000..2933e8e4 --- /dev/null +++ b/server/config/db_list.go @@ -0,0 +1,21 @@ +package config + +type DB struct { + Disable bool `mapstructure:"disable" json:"disable" yaml:"disable"` + Type string `mapstructure:"type" json:"type" yaml:"type"` + AliasName string `mapstructure:"alias-name" json:"alias-name" yaml:"alias-name"` + 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"` +} + +func (m *DB) Dsn() string { + return m.Username + ":" + m.Password + "@tcp(" + m.Path + ":" + m.Port + ")/" + m.Dbname + "?" + m.Config +} diff --git a/server/global/global.go b/server/global/global.go index 9093c505..e0f40c42 100644 --- a/server/global/global.go +++ b/server/global/global.go @@ -1,6 +1,8 @@ package global import ( + "sync" + "github.com/flipped-aurora/gin-vue-admin/server/utils/timer" "github.com/songzhibin97/gkit/cache/local_cache" @@ -17,6 +19,7 @@ import ( var ( GVA_DB *gorm.DB + GVA_DBList map[string]*gorm.DB GVA_REDIS *redis.Client GVA_CONFIG config.Server GVA_VP *viper.Viper @@ -26,4 +29,23 @@ var ( GVA_Concurrency_Control = &singleflight.Group{} BlackCache local_cache.Cache + lock sync.RWMutex ) + +// GetGlobalDBByDBName 通过名称获取db list中的db +func GetGlobalDBByDBName(dbname string) *gorm.DB { + lock.RLock() + defer lock.RUnlock() + return GVA_DBList[dbname] +} + +// MustGetGlobalDBByDBName 通过名称获取db 如果不存在则panic +func MustGetGlobalDBByDBName(dbname string) *gorm.DB { + lock.RLock() + defer lock.RUnlock() + db, ok := GVA_DBList[dbname] + if !ok || db == nil { + panic("db no init") + } + return db +} diff --git a/server/initialize/db_list.go b/server/initialize/db_list.go new file mode 100644 index 00000000..395d2834 --- /dev/null +++ b/server/initialize/db_list.go @@ -0,0 +1,24 @@ +package initialize + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "gorm.io/gorm" +) + +func DBList() { + dbMap := make(map[string]*gorm.DB) + for _, info := range global.GVA_CONFIG.DBList { + if info.Disable { + continue + } + switch info.Type { + case "mysql": + dbMap[info.Dbname] = GormMysqlByConfig(info) + case "pgsql": + dbMap[info.Dbname] = GormPgSqlByConfig(info) + default: + continue + } + } + global.GVA_DBList = dbMap +} diff --git a/server/initialize/gorm_mysql.go b/server/initialize/gorm_mysql.go index 851fa8de..4c8b6e6f 100644 --- a/server/initialize/gorm_mysql.go +++ b/server/initialize/gorm_mysql.go @@ -1,6 +1,7 @@ package initialize import ( + "github.com/flipped-aurora/gin-vue-admin/server/config" "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/initialize/internal" "gorm.io/driver/mysql" @@ -29,3 +30,23 @@ func GormMysql() *gorm.DB { return db } } + +// GormMysqlByConfig 初始化Mysql数据库用过传入配置 +func GormMysqlByConfig(m config.DB) *gorm.DB { + if m.Dbname == "" { + return nil + } + mysqlConfig := mysql.Config{ + DSN: m.Dsn(), // DSN data source name + DefaultStringSize: 191, // string 类型字段的默认长度 + SkipInitializeWithVersion: false, // 根据版本自动配置 + } + if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config()); err != nil { + panic(err) + } else { + sqlDB, _ := db.DB() + sqlDB.SetMaxIdleConns(m.MaxIdleConns) + sqlDB.SetMaxOpenConns(m.MaxOpenConns) + return db + } +} diff --git a/server/initialize/gorm_pgsql.go b/server/initialize/gorm_pgsql.go index e178a277..e474b981 100644 --- a/server/initialize/gorm_pgsql.go +++ b/server/initialize/gorm_pgsql.go @@ -1,6 +1,7 @@ package initialize import ( + "github.com/flipped-aurora/gin-vue-admin/server/config" "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/initialize/internal" "gorm.io/driver/postgres" @@ -28,3 +29,22 @@ func GormPgSql() *gorm.DB { return db } } + +// GormPgSqlByConfig 初始化 Postgresql 数据库 通过参数 +func GormPgSqlByConfig(p config.DB) *gorm.DB { + if p.Dbname == "" { + return nil + } + pgsqlConfig := postgres.Config{ + DSN: p.Dsn(), // DSN data source name + PreferSimpleProtocol: false, + } + if db, err := gorm.Open(postgres.New(pgsqlConfig), internal.Gorm.Config()); err != nil { + panic(err) + } else { + sqlDB, _ := db.DB() + sqlDB.SetMaxIdleConns(p.MaxIdleConns) + sqlDB.SetMaxOpenConns(p.MaxOpenConns) + return db + } +} diff --git a/server/main.go b/server/main.go index e607749a..cdcab9ed 100644 --- a/server/main.go +++ b/server/main.go @@ -23,6 +23,7 @@ func main() { global.GVA_LOG = core.Zap() // 初始化zap日志库 global.GVA_DB = initialize.Gorm() // gorm连接数据库 initialize.Timer() + initialize.DBList() if global.GVA_DB != nil { initialize.RegisterTables(global.GVA_DB) // 初始化表 // 程序结束前关闭数据库链接 From aac4ff2d871bbc3769947bda3466438e8d7daa4c Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Wed, 8 Dec 2021 12:57:15 +0800 Subject: [PATCH 3/5] =?UTF-8?q?feat:=20=E5=A4=9A=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E9=80=82=E9=85=8D=E4=BD=8E=E7=89=88=E6=9C=AC=E8=BF=81?= =?UTF-8?q?=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/initialize/db_list.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/server/initialize/db_list.go b/server/initialize/db_list.go index 395d2834..5b20456e 100644 --- a/server/initialize/db_list.go +++ b/server/initialize/db_list.go @@ -5,6 +5,8 @@ import ( "gorm.io/gorm" ) +const system = "system" + func DBList() { dbMap := make(map[string]*gorm.DB) for _, info := range global.GVA_CONFIG.DBList { @@ -20,5 +22,10 @@ func DBList() { continue } } + // 做特殊判断,是否有迁移 + // 适配低版本迁移多数据库版本 + if sysDB, ok := dbMap[system]; ok { + global.GVA_DB = sysDB + } global.GVA_DBList = dbMap } From 6f813645f21c66fa443eb189b8829907de46823f Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Wed, 8 Dec 2021 13:16:25 +0800 Subject: [PATCH 4/5] fix:conflict --- server/initialize/db_list.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/initialize/db_list.go b/server/initialize/db_list.go index 5b20456e..d976b12c 100644 --- a/server/initialize/db_list.go +++ b/server/initialize/db_list.go @@ -5,7 +5,7 @@ import ( "gorm.io/gorm" ) -const system = "system" +const sys = "system" func DBList() { dbMap := make(map[string]*gorm.DB) @@ -24,7 +24,7 @@ func DBList() { } // 做特殊判断,是否有迁移 // 适配低版本迁移多数据库版本 - if sysDB, ok := dbMap[system]; ok { + if sysDB, ok := dbMap[sys]; ok { global.GVA_DB = sysDB } global.GVA_DBList = dbMap From d63de10979ff8b8d90ea8cffb335c97fecaaf53d Mon Sep 17 00:00:00 2001 From: ShadowWalker Date: Fri, 10 Dec 2021 09:37:14 +0800 Subject: [PATCH 5/5] =?UTF-8?q?fixed:=20=E7=B3=BB=E7=BB=9F=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E4=B8=ADOSS=E7=B1=BB=E5=9E=8B=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E7=9A=84=E6=B3=A8=E9=87=8A=E9=94=99=E8=AF=AF;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config.docker.yaml | 2 +- server/config.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/config.docker.yaml b/server/config.docker.yaml index 04b79b77..a5a72c50 100644 --- a/server/config.docker.yaml +++ b/server/config.docker.yaml @@ -43,7 +43,7 @@ system: env: 'public' # Change to "develop" to skip authentication for development mode addr: 8888 db-type: 'mysql' - oss-type: 'local' # 控制oss选择走本期还是 七牛等其他仓 自行增加其他oss仓可以在 server/utils/upload/upload.go 中 NewOss函数配置 + oss-type: 'local' # 控制oss选择走本地还是 七牛等其他仓 自行增加其他oss仓可以在 server/utils/upload/upload.go 中 NewOss函数配置 use-multipoint: false # captcha configuration diff --git a/server/config.yaml b/server/config.yaml index 1946fdb8..97b0bcc6 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -42,7 +42,7 @@ system: env: 'public' # Change to "develop" to skip authentication for development mode addr: 8888 db-type: 'mysql' - oss-type: 'local' # 控制oss选择走本期还是 七牛等其他仓 自行增加其他oss仓可以在 server/utils/upload/upload.go 中 NewOss函数配置 + oss-type: 'local' # 控制oss选择走本地还是 七牛等其他仓 自行增加其他oss仓可以在 server/utils/upload/upload.go 中 NewOss函数配置 use-multipoint: false # IP限制次数 一个小时15000次 iplimit-count: 15000