Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
bbc4005a
提交
bbc4005a
authored
7月 23, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
加入hashUtils
上级
9b25eae5
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
56 行增加
和
23 行删除
+56
-23
httpcli.go
httpcli/httpcli.go
+27
-17
dxsf.go
utils/dxsf/dxsf.go
+11
-6
md5.go
utils/hashUtils/md5.go
+18
-0
没有找到文件。
httpcli/httpcli.go
浏览文件 @
bbc4005a
...
...
@@ -7,14 +7,15 @@ import (
"encoding/json"
"errors"
"fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/entity"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/httpContentType"
"io"
"net/http"
"net/url"
"strings"
"time"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/entity"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/httpContentType"
"github.com/duke-git/lancet/v2/retry"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
...
...
@@ -36,6 +37,8 @@ type Request struct {
request
*
http
.
Request
response
*
Response
err
error
omitLog
bool
}
// Response HTTP response
...
...
@@ -134,6 +137,10 @@ func (x *Request) SetRetry(count uint) *Request {
x
.
retryCount
=
count
return
x
}
func
(
x
*
Request
)
OmitLog
()
*
Request
{
x
.
omitLog
=
true
return
x
}
// SetContentType set ContentType
func
(
x
*
Request
)
SetContentType
(
a
string
)
*
Request
{
...
...
@@ -275,14 +282,16 @@ func (x *Request) send(ctx context.Context) {
}
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
x
.
timeout
)
defer
cancel
()
x
.
request
.
WithContext
(
ctx
)
x
.
request
=
x
.
request
.
WithContext
(
ctx
)
logger
.
Info
(
"[httpCli] req"
,
logger
.
String
(
"method"
,
x
.
method
),
logger
.
String
(
"url"
,
x
.
url
),
logger
.
Any
(
"header"
,
x
.
request
.
Header
),
logger
.
String
(
"body"
,
bodyBuf
.
String
()),
ctxUtils
.
CtxTraceIDField
(
ctx
))
if
!
x
.
omitLog
{
logger
.
Info
(
"[httpCli] req"
,
logger
.
String
(
"method"
,
x
.
method
),
logger
.
String
(
"url"
,
x
.
url
),
logger
.
Any
(
"header"
,
x
.
request
.
Header
),
logger
.
String
(
"body"
,
bodyBuf
.
String
()),
ctxUtils
.
CtxTraceIDField
(
ctx
))
}
st
:=
time
.
Now
()
if
x
.
retryCount
>
0
{
...
...
@@ -302,14 +311,15 @@ func (x *Request) send(ctx context.Context) {
if
httpContentType
.
IsMedia
(
x
.
response
.
Header
)
{
body
=
string
(
getResponseBody
(
x
.
Response
()
.
BodyBuf
(),
16
))
}
logger
.
Info
(
"[httpCli] rsp"
,
logger
.
String
(
"cost"
,
time
.
Since
(
st
)
.
String
()),
logger
.
Int
(
"statusCode"
,
x
.
response
.
StatusCode
),
logger
.
String
(
"status"
,
x
.
response
.
Status
),
logger
.
Any
(
"header"
,
x
.
Response
()
.
Header
),
logger
.
String
(
"body"
,
body
),
ctxUtils
.
CtxTraceIDField
(
ctx
))
if
!
x
.
omitLog
{
logger
.
Info
(
"[httpCli] rsp"
,
logger
.
String
(
"cost"
,
time
.
Since
(
st
)
.
String
()),
logger
.
Int
(
"statusCode"
,
x
.
response
.
StatusCode
),
logger
.
String
(
"status"
,
x
.
response
.
Status
),
logger
.
Any
(
"header"
,
x
.
Response
()
.
Header
),
logger
.
String
(
"body"
,
body
),
ctxUtils
.
CtxTraceIDField
(
ctx
))
}
}
// If there is sensitive information in the body, you can use WithIgnoreRoutes set the route to ignore logging
...
...
utils/dxsf/dxsf.go
浏览文件 @
bbc4005a
...
...
@@ -13,8 +13,8 @@ import (
)
var
(
xsfAddr
string
httpClient
=
&
client
{
xsfAddr
string
api
=
&
client
{
cli
:
httpcli
.
NewClient
(
&
http
.
Client
{
Transport
:
&
http
.
Transport
{
...
...
@@ -24,6 +24,7 @@ var (
},
}),
}
ErrNoXsfAddr
=
errors
.
New
(
"缺少环境变量XSF_ADDR"
)
)
type
client
struct
{
...
...
@@ -35,21 +36,25 @@ func init() {
}
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
)
reply
:=
&
IDReply
{}
if
err
:=
httpClient
.
cli
.
NewRequest
()
.
SetRetry
(
3
)
.
SetContentType
(
"application/json"
)
.
SetURL
(
url
)
.
GET
(
ctx
)
.
BindJSON
(
reply
)
.
Err
();
err
!=
nil
{
return
xsf
.
ID
(
0
),
err
if
err
:=
api
.
cli
.
NewRequest
()
.
OmitLog
()
.
SetRetry
(
3
)
.
SetContentType
(
"application/json"
)
.
SetURL
(
url
)
.
GET
(
ctx
)
.
BindJSON
(
reply
)
.
Err
();
err
!=
nil
{
return
xsf
.
ID
(
-
1
),
err
}
if
reply
.
Code
==
0
{
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
{
Code
int
`json:"code"`
Data
struct
{
ID
int64
`json:"
ID
"`
ID
int64
`json:"
id
"`
}
`json:"data"`
Msg
string
`json:"msg"`
}
utils/hashUtils/md5.go
0 → 100644
浏览文件 @
bbc4005a
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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论