提交 bbc4005a authored 作者: mooncake9527's avatar mooncake9527

加入hashUtils

上级 9b25eae5
...@@ -7,14 +7,15 @@ import ( ...@@ -7,14 +7,15 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/entity"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/httpContentType"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"time" "time"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/entity"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/httpContentType"
"github.com/duke-git/lancet/v2/retry" "github.com/duke-git/lancet/v2/retry"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils" "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/logger" "gitlab.wanzhuangkj.com/tush/xpkg/logger"
...@@ -36,6 +37,8 @@ type Request struct { ...@@ -36,6 +37,8 @@ type Request struct {
request *http.Request request *http.Request
response *Response response *Response
err error err error
omitLog bool
} }
// Response HTTP response // Response HTTP response
...@@ -134,6 +137,10 @@ func (x *Request) SetRetry(count uint) *Request { ...@@ -134,6 +137,10 @@ func (x *Request) SetRetry(count uint) *Request {
x.retryCount = count x.retryCount = count
return x return x
} }
func (x *Request) OmitLog() *Request {
x.omitLog = true
return x
}
// SetContentType set ContentType // SetContentType set ContentType
func (x *Request) SetContentType(a string) *Request { func (x *Request) SetContentType(a string) *Request {
...@@ -275,14 +282,16 @@ func (x *Request) send(ctx context.Context) { ...@@ -275,14 +282,16 @@ func (x *Request) send(ctx context.Context) {
} }
ctx, cancel := context.WithTimeout(ctx, x.timeout) ctx, cancel := context.WithTimeout(ctx, x.timeout)
defer cancel() defer cancel()
x.request.WithContext(ctx) x.request = x.request.WithContext(ctx)
logger.Info("[httpCli] req", if !x.omitLog {
logger.String("method", x.method), logger.Info("[httpCli] req",
logger.String("url", x.url), logger.String("method", x.method),
logger.Any("header", x.request.Header), logger.String("url", x.url),
logger.String("body", bodyBuf.String()), logger.Any("header", x.request.Header),
ctxUtils.CtxTraceIDField(ctx)) logger.String("body", bodyBuf.String()),
ctxUtils.CtxTraceIDField(ctx))
}
st := time.Now() st := time.Now()
if x.retryCount > 0 { if x.retryCount > 0 {
...@@ -302,14 +311,15 @@ func (x *Request) send(ctx context.Context) { ...@@ -302,14 +311,15 @@ func (x *Request) send(ctx context.Context) {
if httpContentType.IsMedia(x.response.Header) { if httpContentType.IsMedia(x.response.Header) {
body = string(getResponseBody(x.Response().BodyBuf(), 16)) body = string(getResponseBody(x.Response().BodyBuf(), 16))
} }
if !x.omitLog {
logger.Info("[httpCli] rsp", logger.Info("[httpCli] rsp",
logger.String("cost", time.Since(st).String()), logger.String("cost", time.Since(st).String()),
logger.Int("statusCode", x.response.StatusCode), logger.Int("statusCode", x.response.StatusCode),
logger.String("status", x.response.Status), logger.String("status", x.response.Status),
logger.Any("header", x.Response().Header), logger.Any("header", x.Response().Header),
logger.String("body", body), logger.String("body", body),
ctxUtils.CtxTraceIDField(ctx)) ctxUtils.CtxTraceIDField(ctx))
}
} }
// If there is sensitive information in the body, you can use WithIgnoreRoutes set the route to ignore logging // If there is sensitive information in the body, you can use WithIgnoreRoutes set the route to ignore logging
......
...@@ -13,8 +13,8 @@ import ( ...@@ -13,8 +13,8 @@ import (
) )
var ( var (
xsfAddr string xsfAddr string
httpClient = &client{ api = &client{
cli: httpcli.NewClient( cli: httpcli.NewClient(
&http.Client{ &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
...@@ -24,6 +24,7 @@ var ( ...@@ -24,6 +24,7 @@ var (
}, },
}), }),
} }
ErrNoXsfAddr = errors.New("缺少环境变量XSF_ADDR")
) )
type client struct { type client struct {
...@@ -35,21 +36,25 @@ func init() { ...@@ -35,21 +36,25 @@ func init() {
} }
func GenerateApiID(ctx context.Context, bizTag string) (xsf.ID, error) { func GenerateApiID(ctx context.Context, bizTag string) (xsf.ID, error) {
if xsfAddr == "" {
return xsf.ID(-1), ErrNoXsfAddr
}
url := fmt.Sprintf(`http://%s/api/leaf?biz_tag=%s`, xsfAddr, bizTag) url := fmt.Sprintf(`http://%s/api/leaf?biz_tag=%s`, xsfAddr, bizTag)
reply := &IDReply{} reply := &IDReply{}
if err := httpClient.cli.NewRequest().SetRetry(3).SetContentType("application/json").SetURL(url).GET(ctx).BindJSON(reply).Err(); err != nil { if err := api.cli.NewRequest().OmitLog().SetRetry(3).
return xsf.ID(0), err SetContentType("application/json").SetURL(url).GET(ctx).BindJSON(reply).Err(); err != nil {
return xsf.ID(-1), err
} }
if reply.Code == 0 { if reply.Code == 0 {
return xsf.ParseInt64(reply.Data.ID), nil return xsf.ParseInt64(reply.Data.ID), nil
} }
return xsf.ID(0), errors.New(reply.Msg) return xsf.ID(-1), errors.New(reply.Msg)
} }
type IDReply struct { type IDReply struct {
Code int `json:"code"` Code int `json:"code"`
Data struct { Data struct {
ID int64 `json:"ID"` ID int64 `json:"id"`
} `json:"data"` } `json:"data"`
Msg string `json:"msg"` Msg string `json:"msg"`
} }
package hashUtils
import (
"crypto/md5"
"encoding/hex"
)
func Md5(d []byte) string {
hashed := md5.Sum(d)
return hex.EncodeToString(hashed[:])
}
func Md5Stream(d []byte) string {
hash := md5.New()
hash.Write(d)
hashed := hash.Sum(nil)
return hex.EncodeToString(hashed)
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论