187 lines
3.6 KiB
Go
187 lines
3.6 KiB
Go
package responsex
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
)
|
||
|
||
type options struct {
|
||
logger Logger
|
||
ignoreLog bool
|
||
|
||
traceFunc func(ctx context.Context) string
|
||
|
||
defaultSuccessCode int
|
||
defaultErrorCode int
|
||
defaultSuccessMessage string
|
||
successMsgSet bool
|
||
|
||
code int
|
||
data any
|
||
message string
|
||
pagination *Pagination
|
||
httpStatusCode int
|
||
headers map[string]string
|
||
}
|
||
|
||
var op *options = nil
|
||
|
||
func init() {
|
||
op = defaultOptions()
|
||
}
|
||
|
||
func InitOptions(ops ...Option) {
|
||
for _, o := range ops {
|
||
o(op)
|
||
}
|
||
}
|
||
|
||
func defaultOptions() *options {
|
||
return &options{
|
||
logger: &defaultLogger{},
|
||
|
||
traceFunc: func(ctx context.Context) string {
|
||
val := ctx.Value("trace_id")
|
||
v, ok := val.(string)
|
||
if ok {
|
||
return v
|
||
}
|
||
return ""
|
||
},
|
||
|
||
defaultSuccessCode: 200,
|
||
defaultErrorCode: 400,
|
||
defaultSuccessMessage: "请求成功",
|
||
|
||
code: 200,
|
||
data: nil, // 默认为nil
|
||
message: "请求成功",
|
||
}
|
||
}
|
||
|
||
type Option func(*options)
|
||
|
||
func SetCode(code int) Option {
|
||
return func(o *options) {
|
||
o.code = code
|
||
}
|
||
}
|
||
|
||
func SetData(data any) Option {
|
||
return func(o *options) {
|
||
o.data = data
|
||
}
|
||
}
|
||
|
||
func SetMessage(message string) Option {
|
||
return func(o *options) {
|
||
o.message = message
|
||
}
|
||
}
|
||
|
||
func SetPage(page *Pagination) Option {
|
||
return func(o *options) {
|
||
o.pagination = page
|
||
}
|
||
}
|
||
|
||
// 设置默认的成功code
|
||
func SetDefaultSuccessCode(code int) Option {
|
||
return func(o *options) {
|
||
o.defaultSuccessCode = code
|
||
}
|
||
}
|
||
|
||
// 设置默认的成功文案(多语言场景见 SuccessMessageCtx)
|
||
// 传回内置默认文案 "请求成功" 即恢复为"未显式配置",交还给多语言解析
|
||
func SetDefaultSuccessMessage(message string) Option {
|
||
return func(o *options) {
|
||
o.defaultSuccessMessage = message
|
||
o.successMsgSet = message != defaultSuccessMessage
|
||
}
|
||
}
|
||
|
||
// 设置默认的失败code
|
||
func SetDefaultErrorCode(code int) Option {
|
||
return func(o *options) {
|
||
o.defaultErrorCode = code
|
||
}
|
||
}
|
||
|
||
// 是否需要打印日志
|
||
func SetIgnoreLog() Option {
|
||
return func(o *options) {
|
||
o.ignoreLog = true
|
||
}
|
||
}
|
||
|
||
// 设置日志打印器
|
||
func SetLogger(logger Logger) Option {
|
||
return func(o *options) {
|
||
o.logger = logger
|
||
}
|
||
}
|
||
|
||
func SetTraceFunc(f func(ctx context.Context) string) Option {
|
||
return func(o *options) {
|
||
o.traceFunc = f
|
||
}
|
||
}
|
||
|
||
// 设置trace_id的ctx key(默认已用标准key CtxTraceIDKey,一般无需调用)
|
||
func SetTraceKey(traceKey any) Option {
|
||
return func(o *options) {
|
||
o.traceFunc = func(ctx context.Context) string {
|
||
return ctx.Value(traceKey).(string)
|
||
}
|
||
}
|
||
}
|
||
|
||
// 设置HTTP状态码
|
||
func SetHttpStatusCode(code int) Option {
|
||
return func(o *options) {
|
||
o.httpStatusCode = code
|
||
}
|
||
}
|
||
|
||
// 设置单个响应头
|
||
func SetHeader(key, value string) Option {
|
||
return func(o *options) {
|
||
if o.headers == nil {
|
||
o.headers = make(map[string]string)
|
||
}
|
||
o.headers[key] = value
|
||
}
|
||
}
|
||
|
||
// 批量设置响应头
|
||
func SetHeaders(headers map[string]string) Option {
|
||
return func(o *options) {
|
||
o.headers = headers
|
||
}
|
||
}
|
||
|
||
type Logger interface {
|
||
Info(ctx context.Context, args ...interface{})
|
||
Infof(ctx context.Context, format string, args ...interface{})
|
||
Error(ctx context.Context, args ...interface{})
|
||
Errorf(ctx context.Context, format string, args ...interface{})
|
||
}
|
||
|
||
type defaultLogger struct{}
|
||
|
||
func (d defaultLogger) Info(ctx context.Context, args ...interface{}) {
|
||
fmt.Println(args...)
|
||
}
|
||
|
||
func (d *defaultLogger) Infof(ctx context.Context, format string, args ...interface{}) {
|
||
fmt.Printf(format, args...)
|
||
}
|
||
func (d defaultLogger) Error(ctx context.Context, args ...interface{}) {
|
||
fmt.Println(args...)
|
||
}
|
||
|
||
func (d *defaultLogger) Errorf(ctx context.Context, format string, args ...interface{}) {
|
||
fmt.Printf(format, args...)
|
||
}
|