Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
b9c4159d
提交
b9c4159d
authored
9月 18, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
upd
上级
555bac62
显示空白字符变更
内嵌
并排
正在显示
15 个修改的文件
包含
89 行增加
和
64 行删除
+89
-64
xpkg.yaml
conf/xpkg.yaml
+8
-2
config.go
config/config.go
+16
-11
init.go
config/init.go
+3
-0
consts.go
consts/consts.go
+4
-4
init.go
database/init.go
+15
-17
mysql.go
database/mysql.go
+1
-1
redis.go
database/redis.go
+1
-1
init.go
eventbus/init.go
+1
-0
xgin.go
gin/xgin/xgin.go
+6
-6
init.go
init.go
+3
-3
webLogUtils.go
utils/webLogUtils/webLogUtils.go
+10
-4
cron_job.go
xcron/dao/cron_job.go
+0
-5
cron_job_log.go
xcron/dao/cron_job_log.go
+0
-5
cron_job_record.go
xcron/dao/cron_job_record.go
+0
-5
init.go
xcron/dao/init.go
+21
-0
没有找到文件。
conf/xpkg.yaml
浏览文件 @
b9c4159d
...
...
@@ -12,10 +12,15 @@ app:
enableHTTPProfile
:
true
enableLimit
:
false
enableCircuitBreaker
:
false
enableTrace
:
false
tracingSamplingRate
:
1.0
trace
:
enable
:
false
samplingRate
:
1.0
registryDiscoveryType
:
"
"
cacheType
:
"
memory"
webLog
:
enable
:
true
schema
:
operator
ignorePaths
:
ipRateLimiter
:
enable
:
true
window
:
1m
...
...
@@ -99,6 +104,7 @@ auth:
cron
:
enable
:
true
schema
:
operator
oss
:
endpoint
:
https://oss-cn-hangzhou.aliyuncs.com
...
...
config/config.go
浏览文件 @
b9c4159d
...
...
@@ -53,7 +53,7 @@ func Get() *Config {
func
CronOpen
()
bool
{
enable
:=
false
Read
(
func
(
c
*
Config
)
{
enable
=
c
.
Cron
Jobs
.
Enable
enable
=
c
.
Cron
.
Enable
})
return
enable
}
...
...
@@ -110,7 +110,7 @@ type Config struct {
Jaeger
Jaeger
`yaml:"jaeger" json:"jaeger"`
Logger
Logger
`yaml:"logger" json:"logger"`
Redis
Redis
`yaml:"redis" json:"redis"`
Cron
Jobs
Cron
`yaml:"cron" json:"cron"`
Cron
Cron
`yaml:"cron" json:"cron"`
Oss
oss
.
AliOssConfig
`yaml:"oss" json:"oss"`
Content
[]
byte
`yaml:"-" json:"-"`
}
...
...
@@ -216,17 +216,11 @@ type Email struct {
type
App
struct
{
CacheType
string
`yaml:"cacheType" json:"cacheType"`
Middleware
Middleware
`yaml:"middleware" json:"middleware"`
EnableCircuitBreaker
bool
`yaml:"enableCircuitBreaker" json:"enableCircuitBreaker"`
EnableHTTPProfile
bool
`yaml:"enableHTTPProfile" json:"enableHTTPProfile"`
EnableLimit
bool
`yaml:"enableLimit" json:"enableLimit"`
EnableMetrics
bool
`yaml:"enableMetrics" json:"enableMetrics"`
EnableStat
bool
`yaml:"enableStat" json:"enableStat"`
EnableTrace
bool
`yaml:"enableTrace" json:"enableTrace"`
Env
string
`yaml:"env" json:"env"`
Host
string
`yaml:"host" json:"host"`
Name
string
`yaml:"name" json:"name"`
RegistryDiscoveryType
string
`yaml:"registryDiscoveryType" json:"registryDiscoveryType"`
// e.g. nacos, consul , close if empty
TracingSamplingRate
float64
`yaml:"tracingSamplingRate" json:"tracingSamplingRate"`
Version
string
`yaml:"version" json:"version"`
IPRateLimiter
IPRateLimiter
`yaml:"ipRateLimiter" json:"ipRateLimiter"`
...
...
@@ -241,6 +235,12 @@ type App struct {
type
Middleware
struct
{
Cors
bool
`yaml:"cors" json:"cors"`
RequestID
RequestID
`yaml:"requestID" json:"requestID"`
EnableCircuitBreaker
bool
`yaml:"enableCircuitBreaker" json:"enableCircuitBreaker"`
EnableHTTPProfile
bool
`yaml:"enableHTTPProfile" json:"enableHTTPProfile"`
EnableLimit
bool
`yaml:"enableLimit" json:"enableLimit"`
EnableMetrics
bool
`yaml:"enableMetrics" json:"enableMetrics"`
EnableStat
bool
`yaml:"enableStat" json:"enableStat"`
Trace
Trace
`yaml:"trace" json:"trace"`
}
type
RequestID
struct
{
...
...
@@ -255,9 +255,15 @@ type IDGeneration struct {
type
WebLog
struct
{
Enable
bool
`yaml:"enable" json:"enable"`
Schema
string
`yaml:"schema" json:"schema"`
IgnorePaths
[]
string
`yaml:"ignorePaths" json:"ignorePaths"`
}
type
Trace
struct
{
Enable
bool
`yaml:"enable" json:"enable"`
SamplingRate
float64
`yaml:"samplingRate" json:"samplingRate"`
}
type
IPRateLimiter
struct
{
Enable
bool
`yaml:"enable" json:"enable"`
Sync
bool
`yaml:"sync" json:"sync"`
...
...
@@ -341,7 +347,6 @@ type Redis struct {
type
Database
struct
{
Name
string
`yaml:"name" json:"name"`
Driver
string
`yaml:"driver" json:"driver"`
Default
bool
`yaml:"default" json:"default"`
Mongodb
Mongodb
`yaml:"mongodb" json:"mongodb"`
Mysql
Mysql
`yaml:"mysql" json:"mysql"`
Postgresql
Mysql
`yaml:"postgresql" json:"postgresql"`
...
...
@@ -390,8 +395,8 @@ type UpgradeLimit struct {
}
type
Cron
struct
{
// Jobs []CronJob`yaml:"jobs" json:"jobs"`
Enable
bool
`yaml:"enable" json:"enable"`
Schema
string
`yaml:"schema" json:"schema"`
}
type
Leaf
struct
{
...
...
config/init.go
浏览文件 @
b9c4159d
...
...
@@ -2,11 +2,13 @@ package config
import
(
"bytes"
"context"
"os"
"strings"
"github.com/spf13/cast"
"github.com/spf13/viper"
"gitlab.wanzhuangkj.com/tush/xpkg/eventbus"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
)
...
...
@@ -59,6 +61,7 @@ func Parse() (err error) {
return
err
}
}
eventbus
.
Eb
.
Publish
(
context
.
TODO
(),
eventbus
.
TopicParseConfFinish
)
return
nil
}
...
...
consts/consts.go
浏览文件 @
b9c4159d
package
consts
var
(
DefaultSchema
string
//
DefaultSchema string
RootRoute
string
)
func
SetDefaultSchema
(
schema
string
)
{
DefaultSchema
=
schema
}
//
func SetDefaultSchema(schema string) {
//
DefaultSchema = schema
//
}
func
SetRootRoute
(
v
string
)
{
RootRoute
=
v
...
...
database/init.go
浏览文件 @
b9c4159d
...
...
@@ -5,8 +5,6 @@ import (
"strings"
"gitlab.wanzhuangkj.com/tush/xpkg/config"
"gitlab.wanzhuangkj.com/tush/xpkg/consts"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/sgorm"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
)
...
...
@@ -21,21 +19,21 @@ func InitDB() (err error) {
config
.
Read
(
func
(
c
*
config
.
Config
)
{
databases
=
c
.
Database
})
if
len
(
databases
)
==
1
{
consts
.
DefaultSchema
=
databases
[
0
]
.
Name
}
else
{
found
:=
false
for
_
,
db
:=
range
databases
{
if
db
.
Default
{
consts
.
DefaultSchema
=
db
.
Name
found
=
true
}
}
if
!
found
{
consts
.
DefaultSchema
=
databases
[
0
]
.
Name
}
}
logger
.
Info
(
"[database] default schema: "
+
consts
.
DefaultSchema
)
//
if len(databases) == 1 {
//
consts.DefaultSchema = databases[0].Name
//
} else {
//
found := false
//
for _, db := range databases {
//
if db.Default {
//
consts.DefaultSchema = db.Name
//
found = true
//
}
//
}
//
if !found {
//
consts.DefaultSchema = databases[0].Name
//
}
//
}
//
logger.Info("[database] default schema: " + consts.DefaultSchema)
for
_
,
db
:=
range
databases
{
switch
strings
.
ToLower
(
db
.
Driver
)
{
case
sgorm
.
DBDriverMysql
,
sgorm
.
DBDriverTidb
:
...
...
database/mysql.go
浏览文件 @
b9c4159d
...
...
@@ -15,7 +15,7 @@ import (
func
InitMysql
(
dbConfig
*
config
.
Database
)
(
*
sgorm
.
DB
,
error
)
{
enableTrace
:=
false
config
.
Read
(
func
(
c
*
config
.
Config
)
{
enableTrace
=
c
.
App
.
EnableTrac
e
enableTrace
=
c
.
App
.
Middleware
.
Trace
.
Enabl
e
})
opts
:=
[]
mysql
.
Option
{
mysql
.
WithMaxIdleConns
(
dbConfig
.
Mysql
.
MaxIdleConns
),
...
...
database/redis.go
浏览文件 @
b9c4159d
...
...
@@ -61,7 +61,7 @@ func InitRedis() {
enableTrace
:=
false
config
.
Read
(
func
(
c
*
config
.
Config
)
{
copier
.
Copy
(
&
redisCfg
,
&
c
.
Redis
)
enableTrace
=
c
.
App
.
EnableTrac
e
enableTrace
=
c
.
App
.
Middleware
.
Trace
.
Enabl
e
})
opts
:=
[]
goredis
.
Option
{
goredis
.
WithLogger
(
logger
.
Get
()),
...
...
eventbus/init.go
浏览文件 @
b9c4159d
...
...
@@ -3,6 +3,7 @@ package eventbus
var
Eb
=
New
()
const
(
TopicParseConfFinish
string
=
"event:application:conf:init:finish"
// 配置文件解析完毕
TopicDBInitFinish
string
=
"event:application:database:init:finish"
// 数据库连接初始化完成
TopicCacheInitFinish
string
=
"event:application:cache:init:finish"
// cache初始化完成
TopicCronInitFinish
string
=
"event:application:cron:init:finish"
// 定时任务初始化完成
...
...
gin/xgin/xgin.go
浏览文件 @
b9c4159d
...
...
@@ -67,31 +67,31 @@ func New() *gin.Engine {
jwt
.
WithSigningMethod
(
jwt
.
HS256
),
)
}
if
app
.
EnableMetrics
{
if
app
.
Middleware
.
EnableMetrics
{
r
.
Use
(
metrics
.
Metrics
(
r
,
metrics
.
WithIgnoreStatusCodes
(
http
.
StatusNotFound
),
// ignore 404 status codes
))
}
if
app
.
EnableLimit
{
if
app
.
Middleware
.
EnableLimit
{
r
.
Use
(
middleware
.
RateLimit
())
}
if
app
.
EnableCircuitBreaker
{
if
app
.
Middleware
.
EnableCircuitBreaker
{
r
.
Use
(
middleware
.
CircuitBreaker
())
}
if
app
.
EnableTrac
e
{
if
app
.
Middleware
.
Trace
.
Enabl
e
{
r
.
Use
(
middleware
.
Tracing
(
app
.
Name
))
}
if
app
.
EnableHTTPProfile
{
if
app
.
Middleware
.
EnableHTTPProfile
{
prof
.
Register
(
r
,
prof
.
WithIOWaitTime
())
}
binding
.
Validator
=
validator
.
Init
()
r
.
Use
(
xvalidator
.
Translation
())
webLogUtils
.
Init
(
consts
.
DefaultSchema
)
webLogUtils
.
Init
()
if
config
.
IsNotProd
()
{
r
.
GET
(
consts
.
RootRoute
+
"/config"
,
gin
.
WrapF
(
errcode
.
ShowConfig
([]
byte
(
config
.
Show
()))))
...
...
init.go
浏览文件 @
b9c4159d
...
...
@@ -45,19 +45,19 @@ func Init() (err error) {
if
err
:=
initLogger
(
conf
);
err
!=
nil
{
return
err
}
if
conf
.
App
.
EnableTrac
e
{
if
conf
.
App
.
Middleware
.
Trace
.
Enabl
e
{
tracer
.
InitWithConfig
(
conf
.
App
.
Name
,
conf
.
App
.
Env
,
conf
.
App
.
Version
,
conf
.
Jaeger
.
AgentHost
,
strconv
.
Itoa
(
conf
.
Jaeger
.
AgentPort
),
conf
.
App
.
Tracing
SamplingRate
,
conf
.
App
.
Middleware
.
Trace
.
SamplingRate
,
)
logger
.
Info
(
"[tracer] initialized"
)
}
if
conf
.
App
.
EnableStat
{
if
conf
.
App
.
Middleware
.
EnableStat
{
stat
.
Init
(
stat
.
WithLog
(
logger
.
Get
()),
stat
.
WithAlarm
(),
stat
.
WithPrintField
(
logger
.
String
(
"service_name"
,
conf
.
App
.
Name
),
logger
.
String
(
"host"
,
conf
.
App
.
Host
)),
...
...
utils/webLogUtils/webLogUtils.go
浏览文件 @
b9c4159d
...
...
@@ -27,10 +27,15 @@ import (
)
var
(
schema
string
enable
bool
=
false
ignore
=
make
(
map
[
string
]
struct
{})
)
func
SetSchema
(
v
string
)
{
schema
=
v
}
func
WebLogInterceptor
()
func
(
c
*
gin
.
Context
)
{
return
func
(
c
*
gin
.
Context
)
{
if
!
enable
{
...
...
@@ -92,7 +97,7 @@ func GetUserID(ctx context.Context) xsf.ID {
var
merger
*
merge
.
FanIn
func
Init
(
schema
string
)
{
func
Init
()
{
var
webLogCfg
config
.
WebLog
config
.
IsProd
()
config
.
Read
(
func
(
c
*
config
.
Config
)
{
...
...
@@ -101,6 +106,7 @@ func Init(schema string) {
if
!
webLogCfg
.
Enable
{
return
}
schema
=
webLogCfg
.
Schema
isProd
:=
config
.
IsProd
()
for
_
,
p
:=
range
webLogCfg
.
IgnorePaths
{
ignore
[
strings
.
TrimSpace
(
p
)]
=
struct
{}{}
...
...
@@ -127,7 +133,7 @@ func Init(schema string) {
return
nil
}
insertFn
:=
func
()
error
{
if
err
:=
WebLogDao
.
CreateSliceSilent
(
ctx
,
ors
);
err
!=
nil
{
if
err
:=
webLogDaoInstance
.
CreateSliceSilent
(
ctx
,
ors
);
err
!=
nil
{
logger
.
Error
(
"[webLog] insert fail detail"
,
logger
.
Err
(
err
),
ctxUtils
.
CtxTraceIDField
(
ctx
))
return
err
}
...
...
@@ -147,7 +153,7 @@ func Init(schema string) {
merger
.
Start
()
WebLogDao
.
db
=
database
.
DB
(
schema
)
webLogDaoInstance
.
db
=
database
.
DB
(
schema
)
}
func
AddAsync
(
ctx
context
.
Context
,
traceNo
,
operate
string
,
req
*
entity
.
CopyHttpReq
,
rsp
*
entity
.
CopyHttpRsp
)
{
...
...
@@ -173,7 +179,7 @@ type webLogDao struct {
db
*
gorm
.
DB
}
var
WebLogDao
=
&
webLogDao
{}
var
webLogDaoInstance
=
&
webLogDao
{}
func
(
x
*
webLogDao
)
CreateSliceSilent
(
ctx
context
.
Context
,
ors
[]
*
WebLog
)
error
{
return
x
.
db
.
WithContext
(
ctx
)
.
Session
(
&
gorm
.
Session
{
Logger
:
glogger
.
Discard
})
.
Create
(
ors
)
.
Error
...
...
xcron/dao/cron_job.go
浏览文件 @
b9c4159d
...
...
@@ -3,8 +3,6 @@ package dao
import
(
"context"
"gitlab.wanzhuangkj.com/tush/xpkg/consts"
"gitlab.wanzhuangkj.com/tush/xpkg/database"
"gitlab.wanzhuangkj.com/tush/xpkg/xcommon/odao"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/enums"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/models"
...
...
@@ -22,9 +20,6 @@ type cronJobDao struct {
var
CronJobDao
=
&
cronJobDao
{
ODao
:
&
odao
.
ODao
[
models
.
CronJob
]{
XDB
:
&
database
.
XDB
{
Schema
:
consts
.
DefaultSchema
,
},
Sf
:
new
(
singleflight
.
Group
),
},
}
...
...
xcron/dao/cron_job_log.go
浏览文件 @
b9c4159d
...
...
@@ -3,12 +3,10 @@ package dao
import
(
"context"
"gitlab.wanzhuangkj.com/tush/xpkg/consts"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
"gitlab.wanzhuangkj.com/tush/xpkg/xcommon/odao"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/models"
"gitlab.wanzhuangkj.com/tush/xpkg/database"
"golang.org/x/sync/singleflight"
)
...
...
@@ -18,9 +16,6 @@ type cronJobLogDao struct {
var
CronJobLogDao
=
&
cronJobLogDao
{
ODao
:
&
odao
.
ODao
[
models
.
CronJobLog
]{
XDB
:
&
database
.
XDB
{
Schema
:
consts
.
DefaultSchema
,
},
Sf
:
new
(
singleflight
.
Group
),
},
}
...
...
xcron/dao/cron_job_record.go
浏览文件 @
b9c4159d
...
...
@@ -3,8 +3,6 @@ package dao
import
(
"context"
"gitlab.wanzhuangkj.com/tush/xpkg/consts"
"gitlab.wanzhuangkj.com/tush/xpkg/database"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
"gitlab.wanzhuangkj.com/tush/xpkg/xcommon/odao"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/models"
...
...
@@ -18,9 +16,6 @@ type cronJobRecordDao struct {
var
CronJobRecordDao
=
&
cronJobRecordDao
{
ODao
:
&
odao
.
ODao
[
models
.
CronJobRecord
]{
XDB
:
&
database
.
XDB
{
Schema
:
consts
.
DefaultSchema
,
},
Sf
:
new
(
singleflight
.
Group
),
},
}
...
...
xcron/dao/init.go
0 → 100644
浏览文件 @
b9c4159d
package
dao
import
(
"context"
"gitlab.wanzhuangkj.com/tush/xpkg/config"
"gitlab.wanzhuangkj.com/tush/xpkg/database"
"gitlab.wanzhuangkj.com/tush/xpkg/eventbus"
)
func
init
()
{
_
=
eventbus
.
Eb
.
Subscribe
(
eventbus
.
TopicParseConfFinish
,
func
(
ctx
context
.
Context
)
{
schema
:=
""
config
.
Read
(
func
(
c
*
config
.
Config
)
{
schema
=
c
.
Cron
.
Schema
})
CronJobDao
.
ODao
.
XDB
=
&
database
.
XDB
{
Schema
:
schema
}
CronJobLogDao
.
ODao
.
XDB
=
&
database
.
XDB
{
Schema
:
schema
}
CronJobRecordDao
.
ODao
.
XDB
=
&
database
.
XDB
{
Schema
:
schema
}
})
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论