Skip to content

Commit

Permalink
#14 optimize third_party_request
Browse files Browse the repository at this point in the history
  • Loading branch information
xinliangnote committed Mar 13, 2021
1 parent f7b3dba commit 5e5db89
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 153 deletions.
10 changes: 5 additions & 5 deletions internal/api/controller/demo_handler/func_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/xinliangnote/go-gin-api/internal/api/code"
"github.com/xinliangnote/go-gin-api/internal/api/repository/third_party_request/go_gin_api_repo"
"github.com/xinliangnote/go-gin-api/internal/api/third_party_request/go_gin_api"
"github.com/xinliangnote/go-gin-api/internal/pkg/cache"
"github.com/xinliangnote/go-gin-api/internal/pkg/core"
"github.com/xinliangnote/go-gin-api/pkg/errno"
Expand Down Expand Up @@ -34,12 +34,12 @@ type traceResponse []struct {
func (h *handler) Trace() core.HandlerFunc {
return func(c core.Context) {
// 三方请求信息
res1, err := go_gin_api_repo.DemoGet("Tom",
res1, err := go_gin_api.DemoGet("Tom",
httpclient.WithTTL(time.Second*5),
httpclient.WithTrace(c.Trace()),
httpclient.WithLogger(c.Logger()),
httpclient.WithHeader("Authorization", c.GetHeader("Authorization")),
httpclient.WithOnFailedRetry(3, time.Second*1, go_gin_api_repo.DemoGetRetryVerify),
httpclient.WithOnFailedRetry(3, time.Second*1, go_gin_api.DemoGetRetryVerify),
)

if err != nil {
Expand All @@ -56,12 +56,12 @@ func (h *handler) Trace() core.HandlerFunc {
p.Println("res1.Name", res1.Name, p.WithTrace(c.Trace()))

// 三方请求信息
res2, err := go_gin_api_repo.DemoPost("Jack",
res2, err := go_gin_api.DemoPost("Jack",
httpclient.WithTTL(time.Second*5),
httpclient.WithTrace(c.Trace()),
httpclient.WithLogger(c.Logger()),
httpclient.WithHeader("Authorization", c.GetHeader("Authorization")),
httpclient.WithOnFailedRetry(3, time.Second*1, go_gin_api_repo.DemoPostRetryVerify),
httpclient.WithOnFailedRetry(3, time.Second*1, go_gin_api.DemoPostRetryVerify),
)

if err != nil {
Expand Down
9 changes: 1 addition & 8 deletions internal/api/repository/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

- `./db_repo` 访问 DB 数据
- `./cache_repo` 访问 Cache 数据
- `./third_party_request` 访问外部 HTTP 接口数据。

#### SQL 建议:
- 建议每张表需包含字段:主键(id)、标记删除(is_deteled)、创建时间(created_at)、更新时间(updated_at)
Expand All @@ -23,10 +22,4 @@

#### 脚本生成 MySQL CURD

1. 定义生成的表,设置 config 中 cmd.genTables,可以自定义设置多张表,为空表示生成库中所有的表,如果设置多个表可用','分割;
1. 在根目录下执行脚本文件:`./scripts/gormgen.sh`

以用户表(user_demo)为例:
- 结构体文件:user_demo_repo/gen_model.go;
- CURD 方法文件:user_demo_repo/gen_user_demo.go;
- 表结构 MD 文件:user_demo_repo/gen_table.md;
- 使用脚本自动生成基于表结构的 CURD 代码,见文档:`./cmd/gormgen/README.md`

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
package third_party_request

import (
"github.com/xinliangnote/go-gin-api/configs"
"github.com/xinliangnote/go-gin-api/pkg/httpclient"
"github.com/xinliangnote/go-gin-api/pkg/mail"

"github.com/pkg/errors"
)

// 实现 AlarmObject 告警
var _ httpclient.AlarmObject = (*AlarmEmail)(nil)

type AlarmEmail struct{}

// 邮件告警方式
func (a *AlarmEmail) Send(subject, body string) error {
cfg := configs.Get().Mail
if cfg.Host == "" || cfg.Port == 0 || cfg.User == "" || cfg.Pass == "" || cfg.To == "" {
return errors.New("mail config error")
}

options := &mail.Options{
MailHost: "smtp.163.com",
MailPort: 465,
MailUser: "xx@163.com",
MailPass: "",
MailTo: "",
MailHost: cfg.Host,
MailPort: cfg.Port,
MailUser: cfg.User,
MailPass: cfg.Pass,
MailTo: cfg.To,
Subject: subject,
Body: body,
}

return mail.Send(options)
}
63 changes: 63 additions & 0 deletions internal/api/third_party_request/go_gin_api/demoget.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package go_gin_api

import (
"encoding/json"

"github.com/xinliangnote/go-gin-api/pkg/httpclient"

"github.com/pkg/errors"
)

// 接口地址
var demoGetApi = "http://127.0.0.1:9999/demo/get/"

// 接口返回结构
type demoGetResponse struct {
Name string `json:"name"`
Job string `json:"job"`
}

// 发起请求
func DemoGet(name string, opts ...httpclient.Option) (res *demoGetResponse, err error) {
api := demoGetApi + name
body, err := httpclient.Get(api, nil, opts...)
if err != nil {
return nil, err
}

res = new(demoGetResponse)
err = json.Unmarshal(body, res)
if err != nil {
return nil, errors.Wrap(err, "DemoGet json unmarshal error")
}

return res, nil
}

// 设置重试规则
func DemoGetRetryVerify(body []byte) (shouldRetry bool) {
if len(body) == 0 {
return true
}

return false
}

// 设置告警规则
func DemoGetAlarmVerify(body []byte) (shouldAlarm bool) {
if len(body) == 0 {
return true
}

return false
}

// 设置 Mock 数据
func DemoGetMock() (body []byte) {
res := new(demoGetResponse)
res.Name = "AA"
res.Job = "AA_JOB"

body, _ = json.Marshal(res)
return body
}
29 changes: 29 additions & 0 deletions internal/api/third_party_request/go_gin_api/demoget_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package go_gin_api

import (
"testing"
"time"

"github.com/xinliangnote/go-gin-api/internal/api/third_party_request"
"github.com/xinliangnote/go-gin-api/pkg/httpclient"
)

var demoGetAuthorization = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySUQiOjEsIlVzZXJOYW1lIjoieGlubGlhbmdub3RlIiwiZXhwIjoxNjEzODI3MTEzLCJpYXQiOjE2MTM3NDA3MTMsIm5iZiI6MTYxMzc0MDcxM30.SnooP1ikO33ryGPdohsmOKqISa-bWzMkMvUNb5f2zc0"

func TestDemoGet(t *testing.T) {
res, err := DemoGet("Tom",
httpclient.WithTTL(time.Second*5),
//httpclient.WithTrace(ctx.Trace()),
//httpclient.WithLogger(ctx.Logger()),
httpclient.WithHeader("Authorization", demoGetAuthorization),
httpclient.WithOnFailedRetry(3, time.Second*1, DemoGetRetryVerify),
httpclient.WithOnFailedAlarm("接口告警", new(third_party_request.AlarmEmail), DemoGetAlarmVerify),
httpclient.WithMock(DemoGetMock),
)

if err != nil {
t.Log("get [demo/get] err", err)
}

t.Log(res)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package go_gin_api_repo
package go_gin_api

import (
"encoding/json"
Expand All @@ -9,42 +9,18 @@ import (
"github.com/pkg/errors"
)

type demoGetResponse struct {
Name string `json:"name"`
Job string `json:"job"`
}
// 接口地址
var demoPostApi = "http://127.0.0.1:9999/demo/post/"

// 接口返回结构
type demoPostResponse struct {
Name string `json:"name"`
Job string `json:"job"`
}

func DemoGet(name string, opts ...httpclient.Option) (res *demoGetResponse, err error) {
api := "http://127.0.0.1:9999/demo/get/" + name
body, err := httpclient.Get(api, nil, opts...)
if err != nil {
return nil, err
}

res = new(demoGetResponse)
err = json.Unmarshal(body, res)
if err != nil {
return nil, errors.Wrap(err, "DemoGet json unmarshal error")
}

return res, nil
}

func DemoGetRetryVerify(body []byte) (shouldRetry bool) {
if len(body) == 0 {
return true
}

return false
}

// 发起请求
func DemoPost(name string, opts ...httpclient.Option) (res *demoPostResponse, err error) {
api := "http://127.0.0.1:9999/demo/post"
api := demoPostApi
params := url.Values{}
params.Set("name", name)
body, err := httpclient.PostForm(api, params, opts...)
Expand All @@ -61,10 +37,30 @@ func DemoPost(name string, opts ...httpclient.Option) (res *demoPostResponse, er
return res, nil
}

// 设置重试规则
func DemoPostRetryVerify(body []byte) (shouldRetry bool) {
if len(body) == 0 {
return true
}

return false
}

// 设置告警规则
func DemoPostAlarmVerify(body []byte) (shouldAlarm bool) {
if len(body) == 0 {
return true
}

return false
}

// 设置 Mock 数据
func DemoPostMock() (body []byte) {
res := new(demoPostResponse)
res.Name = "BB"
res.Job = "BB_JOB"

body, _ = json.Marshal(res)
return body
}
Loading

0 comments on commit 5e5db89

Please sign in to comment.