Skip to content

Commit

Permalink
新增:webapi组件接入链路追踪
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Oct 15, 2023
1 parent 9d96036 commit 80a29e4
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/apiResponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"encoding/json"
"io"
"time"
)

// ApiResponse 标准的API Response结构
Expand All @@ -13,13 +14,20 @@ type ApiResponse[TData any] struct {
StatusCode int
// 返回消息内容
StatusMessage string
// 接口返回时间
ServerAt time.Time
// 耗时
ElapsedMilliseconds int64
// 链路追踪ID
TraceId int64
// 不同接口返回的值
Data TData
}

// SetData 设置Data字段的值
func (receiver *ApiResponse[TData]) SetData(data TData) {
receiver.Data = data
receiver.ServerAt = time.Now()
}

// ToJson 转成Json
Expand All @@ -41,6 +49,7 @@ func Success[TData any](statusMessage string, data TData) ApiResponse[TData] {
StatusMessage: statusMessage,
StatusCode: 200,
Data: data,
ServerAt: time.Now(),
}
}

Expand All @@ -50,6 +59,7 @@ func Error[TData any](statusMessage string, statusCode int) ApiResponse[TData] {
Status: false,
StatusMessage: statusMessage,
StatusCode: statusCode,
ServerAt: time.Now(),
}
}

Expand All @@ -59,6 +69,7 @@ func Error403[TData any](statusMessage string) ApiResponse[TData] {
Status: false,
StatusMessage: statusMessage,
StatusCode: 403,
ServerAt: time.Now(),
}
}

Expand Down

0 comments on commit 80a29e4

Please sign in to comment.