Skip to content

Commit

Permalink
perf: 改用 Resty 发送请求
Browse files Browse the repository at this point in the history
  • Loading branch information
SugarMGP committed Nov 17, 2024
1 parent e07307b commit fc3b358
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 157 deletions.
9 changes: 5 additions & 4 deletions app/services/userCenterService/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ func Login(stuId string, pass string) error {
}
loginUrl.RawQuery = params.Encode()
urlPath := loginUrl.String()
regMap := make(map[string]any)
regMap["stu_id"] = stuId
regMap["password"] = pass
regMap["bound_system"] = 1
regMap := map[string]any{
"stu_id": stuId,
"password": pass,
"bound_system": 1,
}
resp, err := FetchHandleOfPost(regMap, userCenterApi.UserCenterApi(urlPath))
if err != nil {
return apiException.RequestError
Expand Down
29 changes: 16 additions & 13 deletions app/services/userCenterService/fetch.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package userCenterService

import (
"encoding/json"

"4u-go/app/apiException"
"4u-go/app/utils/fetch"
"4u-go/app/utils/request"
"4u-go/config/api/userCenterApi"
)

Expand All @@ -15,18 +13,23 @@ type UserCenterResponse struct {
Data any `json:"data"`
}

// FetchHandleOfPost 向用户中心发送post请求
// FetchHandleOfPost 向用户中心发送 POST 请求
func FetchHandleOfPost(form map[string]any, url userCenterApi.UserCenterApi) (*UserCenterResponse, error) {
f := fetch.Fetch{}
f.Init()
res, err := f.PostJsonForm(userCenterApi.UserCenterHost+string(url), form)
if err != nil {
return nil, apiException.RequestError
}
rc := UserCenterResponse{}
err = json.Unmarshal(res, &rc)
if err != nil {
client := request.New()
var rc UserCenterResponse

// 发送 POST 请求并自动解析 JSON 响应
resp, err := client.Request().
SetHeader("Content-Type", "application/json").
SetBody(form).
SetResult(&rc).
Post(userCenterApi.UserCenterHost + string(url))

// 检查请求错误
if err != nil || resp.IsError() {
return nil, apiException.RequestError
}

// 返回解析后的响应
return &rc, nil
}
15 changes: 8 additions & 7 deletions app/services/userCenterService/reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ func RegWithoutVerify(stuId string, pass string, iid string, email string, userT
}
userUrl.RawQuery = params.Encode()
urlPath := userUrl.String()
regMap := make(map[string]any)
regMap["stu_id"] = stuId
regMap["password"] = pass
regMap["iid"] = iid
regMap["email"] = email
regMap["type"] = userType
regMap["bound_system"] = 1
regMap := map[string]any{
"stu_id": stuId,
"password": pass,
"iid": iid,
"email": email,
"type": userType,
"bound_system": 1,
}
resp, err := FetchHandleOfPost(regMap, userCenterApi.UserCenterApi(urlPath))
if err != nil {
return err
Expand Down
130 changes: 0 additions & 130 deletions app/utils/fetch/fetch.go

This file was deleted.

54 changes: 54 additions & 0 deletions app/utils/request/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//nolint:all
package request

import (
"crypto/tls"
"time"

"github.com/go-resty/resty/v2"
"go.uber.org/zap"
)

// Client 包装 Resty 客户端
type Client struct {
*resty.Client
}

// New 初始化一个 Resty 客户端
func New() Client {
s := Client{
Client: resty.New().
SetTimeout(10 * time.Second).
SetRetryCount(3).
SetRetryWaitTime(2 * time.Second),
}
// 利用中间件实现请求日志
s.OnAfterResponse(RestyLogMiddleware)
return s
}

// NewUnSafe 初始化一个 Resty 客户端并跳过 TLS 证书验证
func NewUnSafe() Client {
s := New()
s.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
return s
}

// Request 获取一个新的请求实例
func (s Client) Request() *resty.Request {
return s.R().EnableTrace()
}

// RestyLogMiddleware Resty日志中间件
func RestyLogMiddleware(_ *resty.Client, resp *resty.Response) error {
if resp.IsError() {
method := resp.Request.Method
url := resp.Request.URL
zap.L().Warn("请求出现错误",
zap.String("method", method),
zap.String("url", url),
zap.Float64("time_spent", resp.Time().Seconds()),
)
}
return nil
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/gin-contrib/sessions v1.0.1
github.com/gin-gonic/gin v1.10.0
github.com/go-redis/redis/v8 v8.11.5
github.com/go-resty/resty/v2 v2.16.0
github.com/silenceper/wechat/v2 v2.1.7
github.com/spf13/viper v1.19.0
go.uber.org/zap v1.27.0
Expand All @@ -32,7 +33,7 @@ require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.1 // indirect
github.com/go-playground/validator/v10 v10.23.0 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gomodule/redigo v2.0.0+incompatible // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA=
github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-playground/validator/v10 v10.23.0 h1:/PwmTwZhS0dPkav3cdK9kV1FsAmrL8sThn8IHr/sO+o=
github.com/go-playground/validator/v10 v10.23.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-resty/resty/v2 v2.16.0 h1:qpKalHWI2bpp9BIKlyT8TYWEJXOk1NuKbfiT3RRnzWc=
github.com/go-resty/resty/v2 v2.16.0/go.mod h1:0fHAoK7JoBy/Ch36N8VFeMsK7xQOHhvWaC3iOktwmIU=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
Expand Down Expand Up @@ -254,6 +256,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
Expand Down

0 comments on commit fc3b358

Please sign in to comment.