Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
f1fb00e9
提交
f1fb00e9
authored
11月 13, 2025
作者:
mooncake
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update
上级
b72afd30
显示空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
66 行增加
和
4 行删除
+66
-4
init_app.go
application/init_app.go
+3
-1
config.go
config/config.go
+1
-1
database.go
database/database.go
+6
-0
postgres.go
database/postgres.go
+54
-0
dictionary.go
gin/enums/dictionary.go
+1
-1
response.go
gin/response/response.go
+1
-1
没有找到文件。
application/init_app.go
浏览文件 @
f1fb00e9
...
@@ -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
...
...
config/config.go
浏览文件 @
f1fb00e9
...
@@ -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"`
}
}
...
...
database/database.go
浏览文件 @
f1fb00e9
...
@@ -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"
)
...
...
database/postgres.go
0 → 100644
浏览文件 @
f1fb00e9
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
}
gin/enums/dictionary.go
浏览文件 @
f1fb00e9
...
@@ -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
{
...
...
gin/response/response.go
浏览文件 @
f1fb00e9
...
@@ -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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论