优化默认返回码

This commit is contained in:
Yun
2024-07-10 17:32:12 +08:00
parent f092f7e49a
commit d05963c86a
4 changed files with 38 additions and 19 deletions
+23 -5
View File
@@ -6,9 +6,11 @@ import (
)
type options struct {
logger Logger
ignoreLog bool
traceId string
logger Logger
ignoreLog bool
traceId string
defaultSuccessCode int
defaultErrorCode int
}
var op *options = nil
@@ -25,12 +27,28 @@ func InitOptions(ops ...Option) {
func defaultOptions() *options {
return &options{
logger: &defaultLogger{},
logger: &defaultLogger{},
defaultSuccessCode: 200,
defaultErrorCode: 400,
}
}
type Option func(*options)
// 设置默认的成功code
func SetDefaultSuccessCode(code int) Option {
return func(o *options) {
o.defaultSuccessCode = code
}
}
// 设置默认的失败code
func SetDefaultErrorCode(code int) Option {
return func(o *options) {
o.defaultErrorCode = code
}
}
// 是否需要打印日志
func SetIgnoreLog() Option {
return func(o *options) {
@@ -45,7 +63,7 @@ func SetLogger(logger Logger) Option {
}
}
// 是否需要响应trace_id
// 是否需要响应trace_id,&ctx里面的字段
func SetTraceId(traceId string) Option {
return func(o *options) {
o.traceId = traceId