-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
206 lines (161 loc) · 4.79 KB
/
handler.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package template
var (
HandlerFNC = `package handler
import (
"context"
{{.Alias}} "{{.Dir}}/proto/{{.Alias}}"
)
type {{title .Alias}} struct{}
// Call is a single request handler called via client.Call or the generated client code
func (e *{{title .Alias}}) Call(ctx context.Context, req *{{.Alias}}.Request, rsp *{{.Alias}}.Response) error {
rsp.Msg = "Hello " + req.Name
return nil
}
`
HandlerSRV = `package handler
import (
"context"
log "github.com/dreamlu/go-micro/v2/logger"
{{.Alias}} "{{.Dir}}/proto/{{.Alias}}"
)
type {{title .Alias}} struct{}
// Call is a single request handler called via client.Call or the generated client code
func (e *{{title .Alias}}) Call(ctx context.Context, req *{{.Alias}}.Request, rsp *{{.Alias}}.Response) error {
log.Info("Received {{title .Alias}}.Call request")
rsp.Msg = "Hello " + req.Name
return nil
}
// Stream is a server side stream handler called via client.Stream or the generated client code
func (e *{{title .Alias}}) Stream(ctx context.Context, req *{{.Alias}}.StreamingRequest, stream {{.Alias}}.{{title .Alias}}_StreamStream) error {
log.Infof("Received {{title .Alias}}.Stream request with count: %d", req.Count)
for i := 0; i < int(req.Count); i++ {
log.Infof("Responding: %d", i)
if err := stream.Send(&{{.Alias}}.StreamingResponse{
Count: int64(i),
}); err != nil {
return err
}
}
return nil
}
// PingPong is a bidirectional stream handler called via client.Stream or the generated client code
func (e *{{title .Alias}}) PingPong(ctx context.Context, stream {{.Alias}}.{{title .Alias}}_PingPongStream) error {
for {
req, err := stream.Recv()
if err != nil {
return err
}
log.Infof("Got ping %v", req.Stroke)
if err := stream.Send(&{{.Alias}}.Pong{Stroke: req.Stroke}); err != nil {
return err
}
}
}
`
SubscriberFNC = `package subscriber
import (
"context"
log "github.com/dreamlu/go-micro/v2/logger"
{{.Alias}} "{{.Dir}}/proto/{{.Alias}}"
)
type {{title .Alias}} struct{}
func (e *{{title .Alias}}) Handle(ctx context.Context, msg *{{.Alias}}.Message) error {
log.Info("Handler Received message: ", msg.Say)
return nil
}
`
SubscriberSRV = `package subscriber
import (
"context"
log "github.com/dreamlu/go-micro/v2/logger"
{{.Alias}} "{{.Dir}}/proto/{{.Alias}}"
)
type {{title .Alias}} struct{}
func (e *{{title .Alias}}) Handle(ctx context.Context, msg *{{.Alias}}.Message) error {
log.Info("Handler Received message: ", msg.Say)
return nil
}
func Handler(ctx context.Context, msg *{{.Alias}}.Message) error {
log.Info("Function Received message: ", msg.Say)
return nil
}
`
HandlerAPI = `package handler
import (
"context"
"encoding/json"
log "github.com/dreamlu/go-micro/v2/logger"
"{{.Dir}}/client"
"github.com/dreamlu/go-micro/v2/errors"
api "github.com/dreamlu/go-micro/v2/api/proto"
{{.Alias}} "path/to/service/proto/{{.Alias}}"
)
type {{title .Alias}} struct{}
func extractValue(pair *api.Pair) string {
if pair == nil {
return ""
}
if len(pair.Values) == 0 {
return ""
}
return pair.Values[0]
}
// {{title .Alias}}.Call is called by the API as /{{.Alias}}/call with post body {"name": "foo"}
func (e *{{title .Alias}}) Call(ctx context.Context, req *api.Request, rsp *api.Response) error {
log.Info("Received {{title .Alias}}.Call request")
// extract the client from the context
{{.Alias}}Client, ok := client.{{title .Alias}}FromContext(ctx)
if !ok {
return errors.InternalServerError("{{.FQDN}}.{{.Alias}}.call", "{{.Alias}} client not found")
}
// make request
response, err := {{.Alias}}Client.Call(ctx, &{{.Alias}}.Request{
Name: extractValue(req.Post["name"]),
})
if err != nil {
return errors.InternalServerError("{{.FQDN}}.{{.Alias}}.call", err.Error())
}
b, _ := json.Marshal(response)
rsp.StatusCode = 200
rsp.Body = string(b)
return nil
}
`
HandlerWEB = `package handler
import (
"context"
"encoding/json"
"net/http"
"time"
"github.com/dreamlu/go-micro/v2/client"
{{.Alias}} "path/to/service/proto/{{.Alias}}"
)
func {{title .Alias}}Call(w http.ResponseWriter, r *http.Request) {
// decode the incoming request as json
var request map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
http.Error(w, err.Error(), 500)
return
}
// call the backend service
{{.Alias}}Client := {{.Alias}}.New{{title .Alias}}Service("{{.Namespace}}.service.{{.Alias}}", client.DefaultClient)
rsp, err := {{.Alias}}Client.Call(context.TODO(), &{{.Alias}}.Request{
Name: request["name"].(string),
})
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// we want to augment the response
response := map[string]interface{}{
"msg": rsp.Msg,
"ref": time.Now().UnixNano(),
}
// encode and write the response as json
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, err.Error(), 500)
return
}
}
`
)