-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_http.pb.go
120 lines (97 loc) · 2.82 KB
/
example_http.pb.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Code generated by protoc-gen-go-http. DO NOT EDIT.
// versions:
// - protoc-gen-go-http v1.0.2
// - protoc v3.21.1
// source: proto/example/example.proto
package example
import (
context "context"
gin "github.com/gin-gonic/gin"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the kratos package it is being compiled against.
var _ = new(context.Context)
const _ = gin.Version
// 这里定义 handler interface
type ExampleHTTPHandler interface {
Add(context.Context, *AddRequest) (*CommonResponse, error)
Get(context.Context, *GetRequest) (*CommonResponse, error)
}
// RegisterExampleHTTPHandler define http router handle by gin.
// 注册路由 handler
func RegisterExampleHTTPHandler(g *gin.RouterGroup, srv ExampleHTTPHandler) {
g.POST("/v1/add", _Example_Add0_HTTP_Handler(srv))
g.GET("/v1/get", _Example_Get0_HTTP_Handler(srv))
}
// 定义 handler
// 遍历之前解析到所有 rpc 方法信息
func _Example_Add0_HTTP_Handler(srv ExampleHTTPHandler) func(c *gin.Context) {
return func(c *gin.Context) {
var (
err error
in = new(AddRequest)
out = new(CommonResponse)
ctx = context.TODO()
)
if err = c.ShouldBind(in); err != nil {
c.AbortWithStatusJSON(400, gin.H{"err": err.Error()})
return
}
// execute
out, err = srv.Add(ctx, in)
if err != nil {
c.AbortWithStatusJSON(500, gin.H{"err": err.Error()})
return
}
c.JSON(200, out)
}
}
func _Example_Get0_HTTP_Handler(srv ExampleHTTPHandler) func(c *gin.Context) {
return func(c *gin.Context) {
var (
err error
in = new(GetRequest)
out = new(CommonResponse)
ctx = context.TODO()
)
if err = c.ShouldBind(in); err != nil {
c.AbortWithStatusJSON(400, gin.H{"err": err.Error()})
return
}
// execute
out, err = srv.Get(ctx, in)
if err != nil {
c.AbortWithStatusJSON(500, gin.H{"err": err.Error()})
return
}
c.JSON(200, out)
}
}
// Client defines call remote server client and implement selector
type Client interface {
Call(ctx context.Context, req, rsp interface{}) error
}
// ExampleHTTPClient defines call ExampleServer client
type ExampleHTTPClient interface {
Add(context.Context, *AddRequest) (*CommonResponse, error)
Get(context.Context, *GetRequest) (*CommonResponse, error)
}
// ExampleHTTPClientImpl implement ExampleHTTPClient
type ExampleHTTPClientImpl struct {
cli Client
}
func NewExampleHTTPClient(cli Client) ExampleHTTPClient {
return &ExampleHTTPClientImpl{
cli: cli,
}
}
func (c *ExampleHTTPClientImpl) Add(ctx context.Context, req *AddRequest) (resp *CommonResponse, err error) {
resp = new(CommonResponse)
err = c.cli.Call(ctx, req, resp)
return
}
func (c *ExampleHTTPClientImpl) Get(ctx context.Context, req *GetRequest) (resp *CommonResponse, err error) {
resp = new(CommonResponse)
err = c.cli.Call(ctx, req, resp)
return
}