-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy path07-callback.go
68 lines (54 loc) · 1.4 KB
/
07-callback.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/guonaihong/gout"
"math/rand"
"time"
)
type Result struct {
Errmsg string `json:"errmsg"`
ErrCode int `json:"errcode"`
}
// Callback接口用于处理服务段会返回多种数据结构,比如404返回出错html, 200返回json
// 客户端example
func callbackExample() {
r, str404 := Result{}, ""
code := 0
err := gout.GET(":8080").Callback(func(c *gout.Context) (err error) {
switch c.Code {
case 200: //http code为200时,服务端返回的是json 结构
c.BindJSON(&r)
case 404: //http code为404时,服务端返回是html 字符串
c.BindBody(&str404)
}
code = c.Code
return nil
}).Do()
if err != nil {
fmt.Printf("err = %s\n", err)
return
}
fmt.Printf("http code = %d, str404(%s) or json result(%v)\n", code, str404, r)
}
func main() {
go server() //等会起测试服务
time.Sleep(time.Millisecond * 500) //用时间做个等待同步
callbackExample()
}
// 模拟 API网关
func server() {
router := gin.New()
router.GET("/", func(c *gin.Context) {
rand.Seed(time.Now().UnixNano())
x := rand.Intn(2) //使用随机函数模拟某个服务有一定概率出现404
switch x {
case 0: // 模拟 404 找不到资源
c.String(404, "<html> not found </html>")
case 1:
// 正确业务返回结果
c.JSON(200, Result{Errmsg: "ok"})
}
})
router.Run()
}