提交 f1fb00e9 authored 作者: mooncake's avatar mooncake

update

上级 b72afd30
...@@ -38,7 +38,9 @@ func init() { ...@@ -38,7 +38,9 @@ func init() {
} }
}) })
eventbus.Eb.Subscribe(eventbus.TopicDBInit, func(ctx context.Context) { eventbus.Eb.Subscribe(eventbus.TopicDBInit, func(ctx context.Context) {
initDatabase() if err := initDatabase(); err != nil {
logger.Fatal(err.Error())
}
}) })
eventbus.Eb.Subscribe(eventbus.TopicCacheInit, func(ctx context.Context) { eventbus.Eb.Subscribe(eventbus.TopicCacheInit, func(ctx context.Context) {
conf := config.Cfg conf := config.Cfg
......
...@@ -360,7 +360,7 @@ type Database struct { ...@@ -360,7 +360,7 @@ type Database struct {
Driver string `yaml:"driver" json:"driver" mapstructure:"driver"` Driver string `yaml:"driver" json:"driver" mapstructure:"driver"`
// Mongodb Mongodb `yaml:"mongodb" json:"mongodb" mapstructure:"mongodb"` // Mongodb Mongodb `yaml:"mongodb" json:"mongodb" mapstructure:"mongodb"`
Mysql Mysql `yaml:"mysql" json:"mysql" mapstructure:"mysql"` Mysql Mysql `yaml:"mysql" json:"mysql" mapstructure:"mysql"`
// Postgresql Mysql `yaml:"postgresql" json:"postgresql" mapstructure:"postgresql"` Postgresql Postgresql `yaml:"postgresql" json:"postgresql" mapstructure:"postgresql"`
// Sqlite Sqlite `yaml:"sqlite" json:"sqlite" mapstructure:"sqlite"` // Sqlite Sqlite `yaml:"sqlite" json:"sqlite" mapstructure:"sqlite"`
} }
......
...@@ -28,6 +28,12 @@ func InitDB() (err error) { ...@@ -28,6 +28,12 @@ func InitDB() (err error) {
return err return err
} }
gdbs[db.Name] = gdb gdbs[db.Name] = gdb
case sgorm.DBDriverPostgresql:
gdb, err := InitPostgreSQL(&db)
if err != nil {
return err
}
gdbs[db.Name] = gdb
default: default:
return xerror.New("InitDB error, please modify the correct 'database' configuration at yaml file. " + return xerror.New("InitDB error, please modify the correct 'database' configuration at yaml file. " +
"Refer to https://xmall/blob/main/configs/xmall.yml#L85") "Refer to https://xmall/blob/main/configs/xmall.yml#L85")
......
package database
import (
"time"
ctxutils "gitlab.wanzhuangkj.com/tush/xpkg/xutils/ctxutils"
"gitlab.wanzhuangkj.com/tush/xpkg/config"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/sgorm"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/sgorm/postgresql"
utils "gitlab.wanzhuangkj.com/tush/xpkg/xutils"
)
func InitPostgreSQL(dbConfig *config.Database) (*sgorm.DB, error) {
enableTrace := false
config.Read(func(c *config.Config) {
enableTrace = c.App.Middleware.Trace.Enable
})
opts := []postgresql.Option{
postgresql.WithMaxIdleConns(dbConfig.Postgresql.MaxIdleConns),
postgresql.WithMaxOpenConns(dbConfig.Postgresql.MaxOpenConns),
postgresql.WithConnMaxLifetime(time.Duration(dbConfig.Postgresql.ConnMaxLifetime) * time.Minute),
}
if dbConfig.Postgresql.EnableLog {
opts = append(opts,
postgresql.WithLogging(logger.Get()),
postgresql.WithLogRequestIDKey(ctxutils.ContextTraceIDKey),
)
}
if enableTrace {
opts = append(opts, postgresql.WithEnableTrace())
}
// setting postgresql slave and master dsn addresses
// opts = append(opts, postgresql.WithRWSeparation(
// postgresqlCfg.SlavesDsn,
// postgresqlCfg.MastersDsn...,
//))
// add custom gorm plugin
//opts = append(opts, postgresql.WithGormPlugin(yourPlugin))
dsn := utils.AdaptivePostgresqlDsn(dbConfig.Postgresql.Dsn)
db, err := postgresql.Init(dsn, opts...)
if err != nil {
return nil, err
}
sensitiveFields := append([]string{}, `"dsn"`, `"username"`, `"password"`, `"pwd"`, `"signKey"`, `"access-key-id"`, `"access-key-secret"`)
insensitiveDsn := config.HideSensitiveFields(dbConfig.Postgresql.Dsn, sensitiveFields...)
logger.Infof("[database][%s] connected.", insensitiveDsn)
return db, nil
}
...@@ -46,7 +46,7 @@ type BaseReply struct { ...@@ -46,7 +46,7 @@ type BaseReply struct {
Msg string `json:"message" form:"message" example:"OK"` Msg string `json:"message" form:"message" example:"OK"`
Data interface{} `json:"data"` Data interface{} `json:"data"`
TimeStamp int64 `json:"timestamp" form:"data" example:"1718850053"` TimeStamp int64 `json:"timestamp" form:"data" example:"1718850053"`
TraceID string `json:"traceID" form:"traceID" example:"7f0000016673920576f0659c620086b0"` TraceID string `json:"traceID" form:"traceID" example:"1988899757589794816"`
} }
type LabelValue struct { type LabelValue struct {
......
...@@ -55,7 +55,7 @@ type Result struct { ...@@ -55,7 +55,7 @@ type Result struct {
Msg string `json:"message" form:"message" example:"OK"` Msg string `json:"message" form:"message" example:"OK"`
Data interface{} `json:"data"` Data interface{} `json:"data"`
TimeStamp int64 `json:"timestamp" form:"data" example:"1718850053"` TimeStamp int64 `json:"timestamp" form:"data" example:"1718850053"`
TraceID string `json:"traceID" form:"traceID" example:"7f0000016673920576f0659c620086b0"` TraceID string `json:"traceID" form:"traceID" example:"1988899757589794816"`
} }
func newResp(c *gin.Context, code int, msg string, data interface{}) *Result { func newResp(c *gin.Context, code int, msg string, data interface{}) *Result {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论