-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplete.go
131 lines (115 loc) · 3.55 KB
/
templete.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
121
122
123
124
125
126
127
128
129
130
131
package main
// TODO: support validate
var httpCodeTmpl = `
{{/*gotype: github.com/yusank/protoc-gen-go-http.serviceDesc*/}}
{{$svrType := .ServiceType}}
{{$svrName := .ServiceName}}
{{$validate := .GenValidate}}
// {{.ServiceType}}HTTPHandler defines {{.ServiceType}}Server http handler
type {{.ServiceType}}HTTPHandler interface {
{{- range .Methods}}
{{.Name}}(context.Context, *{{.Request}}) (*{{.Reply}}, error)
{{- end}}
}
// Register{{.ServiceType}}HTTPHandler define http router handle by gin.
func Register{{.ServiceType}}HTTPHandler(g *gin.RouterGroup, srv {{.ServiceType}}HTTPHandler) {
{{- range .Methods}}
g.{{.Method}}("{{.Path}}", _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv))
{{- end}}
}
{{if $validate}}
type Validator interface {
Validate() error
}
{{end}}
{{range .Methods}}
// _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler is gin http handler to handle
// http request [{{.Method}}] {{.Path}}.
func _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv {{$svrType}}HTTPHandler) func(c *gin.Context) {
return func(c *gin.Context) {
var (
err error
in = new({{.Request}})
out = new({{.Reply}})
ctx = context.TODO()
)
if err = c.ShouldBind(in{{.Body}}); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"err": err.Error()})
return
}
{{if $validate}}
v,ok := interface{}(in).(Validator)
if ok {
if err = v.Validate();err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"err": err.Error()})
return
}
}
{{end}}
// execute
out, err = srv.{{.Name}}(ctx, in)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"err": err.Error()})
return
}
c.JSON(http.StatusOK, out)
}
}
{{end}}
// {{.ServiceType}}HTTPClient defines call {{.ServiceType}}Server client
type {{.ServiceType}}HTTPClient interface {
{{- range .Methods}}
{{.Name}}(ctx context.Context, req *{{.Request}}, opts ...phttp.CallOption) (*{{.Reply}}, error)
{{- end}}
}
// {{.ServiceType}}HTTPClientImpl implement {{.ServiceType}}HTTPClient
type {{.ServiceType}}HTTPClientImpl struct {
cli *restyv2.Client
clientOpts []phttp.ClientOption
}
func New{{.ServiceType}}HTTPClient(baseUrl string, cli *http.Client, opts ...phttp.ClientOption) ({{.ServiceType}}HTTPClient, error) {
if baseUrl == "" {
return nil, errors.New("base url is empty")
}
c := &{{.ServiceType}}HTTPClientImpl{
clientOpts: opts,
}
hc := cli
if hc == nil {
hc = http.DefaultClient
}
c.cli = restyv2.NewWithClient(hc)
c.cli.SetBaseURL(baseUrl)
for _, opt := range opts {
if err := opt.Apply(c.cli);err != nil {
return nil, err
}
}
return c, nil
}
{{range .Methods}}
// {{.Name}} is call [{{.Method}}] {{.Path}} api.
func (c *{{$svrType}}HTTPClientImpl) {{.Name}}(ctx context.Context, req *{{.Request}}, opts ...phttp.CallOption)(rsp *{{.Reply}} ,err error) {
rsp = new({{.Reply}})
r := c.cli.R()
for _, opt := range opts {
if err = opt.Before(r);err != nil {
return
}
}
// set response data struct.
r.SetResult(rsp)
// do request
restyResp,err := r.Execute("{{.Method}}", "{{.Path}}")
if err != nil {
return nil, err
}
for _, opt := range opts {
if err = opt.After(restyResp);err != nil {
return
}
}
return
}
{{end}}
`