-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_grpc.go
227 lines (206 loc) · 5.43 KB
/
handler_grpc.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package gid_handler
import (
"context"
"fmt"
"math/rand"
"net/http"
"time"
"github.com/google/uuid"
"github.com/mattheath/kala/bigflake"
"github.com/mattheath/kala/snowflake"
"github.com/pubgo/funk/errors"
"github.com/pubgo/funk/log"
"github.com/pubgo/lava/clients/resty"
"github.com/pubgo/lava/core/metrics"
"github.com/pubgo/lava/core/scheduler"
"github.com/pubgo/lava/internal/example/grpc/internal/services/gid_client"
"github.com/pubgo/lava/internal/example/grpc/pkg/proto/gidpb"
"github.com/pubgo/lava/lava"
"github.com/teris-io/shortid"
"google.golang.org/genproto/googleapis/api/httpbody"
"google.golang.org/grpc"
)
var typesReq = &resty.RequestConfig{
Method: http.MethodGet,
Path: "/v1/id/types",
}
var (
_ lava.GrpcRouter = (*Id)(nil)
_ gidpb.IdServer = (*Id)(nil)
)
type Id struct {
cron *scheduler.Scheduler
metric metrics.Metric
snowflake *snowflake.Snowflake
bigflake *bigflake.Bigflake
log log.Logger
service *gid_client.Service
}
func (id *Id) PutTypes(ctx context.Context, req *gidpb.TypesRequest) (*gidpb.TypesResponse, error) {
fmt.Println(req.String() + "\n\n\n\n")
rsp := new(gidpb.TypesResponse)
rsp.Types = []string{
"uuid",
"shortid",
"snowflake",
"bigflake",
}
return rsp, nil
}
func (id *Id) Chat1(server gidpb.Id_Chat1Server) error {
for {
hello, err := server.Recv()
if err != nil {
return err
}
log.Info().Msg(hello.Name)
fmt.Println(server.Send(hello))
}
}
func (id *Id) UploadDownload(ctx context.Context, request *gidpb.UploadFileRequest) (*httpbody.HttpBody, error) {
log.Info().Msg(request.Filename)
return request.File, nil
}
func (id *Id) Chat(server gidpb.Id_ChatServer) error {
for {
hello, err := server.Recv()
if err != nil {
return err
}
log.Info().Msg(hello.Name)
server.Send(hello)
}
}
func (id *Id) TypeStream(request *gidpb.TypesRequest, server gidpb.Id_TypeStreamServer) error {
for i := 0; i < 5; i++ {
rsp := new(gidpb.TypesResponse)
rsp.Types = []string{
"uuid",
"shortid",
"snowflake",
"bigflake",
}
_ = server.Send(rsp)
time.Sleep(time.Second)
}
return nil
}
func (id *Id) Middlewares() []lava.Middleware {
return lava.Middlewares{
lava.MiddlewareWrap{
Next: func(next lava.HandlerFunc) lava.HandlerFunc {
return func(ctx context.Context, req lava.Request) (lava.Response, error) {
id.log.Info().Msgf("middleware %s", req.Endpoint())
fmt.Println(req.Header().String())
return next(ctx, req)
}
},
Name: "header",
},
}
}
func (id *Id) ServiceDesc() *grpc.ServiceDesc {
return &gidpb.Id_ServiceDesc
}
func New(cron *scheduler.Scheduler, metric metrics.Metric, log log.Logger, service *gid_client.Service) lava.GrpcRouter {
id := rand.Intn(100)
sf, err := snowflake.New(uint32(id))
if err != nil {
panic(err.Error())
}
bg, err := bigflake.New(uint64(id))
if err != nil {
panic(err.Error())
}
return &Id{
service: service,
cron: cron,
metric: metric,
snowflake: sf,
bigflake: bg,
log: log.WithName("gid"),
}
}
func (id *Id) Init() {
//id.cron.Every("test_gid", time.Second*10, func(ctx context.Context, name string) error {
// fmt.Println("test cron every")
//
// rsp, err := id.service.Types(ctx, &gidpb.TypesRequest{})
// if err != nil {
// return err
// }
//
// id.log.Info(ctx).Any("data", rsp.Types).Msg("Types")
//
// defer recovery.Exit()
// rsp1 := id.service.Do(ctx, resty.NewRequest(typesReq))
// if rsp1.IsErr() {
// return rsp1.Err()
// }
//
// id.log.Info(ctx).Any("data", string(rsp1.Unwrap().Body())).Msg("Types http")
//
// return nil
//})
}
func (id *Id) Generate(ctx context.Context, req *gidpb.GenerateRequest) (*gidpb.GenerateResponse, error) {
//return nil, errors.New("hello error")
//return nil, errors.NewCodeErr(&errorpb.ErrCode{
// StatusCode: errorpb.Code_Internal,
// Code: 2222,
// Message: "ddd",
// Name: "dd.sss",
//}, gidpb.ErrCodeIDGenerateFailed)
log.Info().Any("type", req.Type.String()).Msg("request")
rsp := new(gidpb.GenerateResponse)
typ := req.GetType().String()
if len(typ) == 0 {
typ = "uuid"
}
switch typ {
case "uuid":
rsp.Type = "uuid"
rsp.Id = uuid.New().String()
case "snowflake":
da, err := id.snowflake.Mint()
if err != nil {
id.log.Err(err).Msg("Failed to generate snowflake id")
err = errors.Wrap(err, "Failed to generate snowflake id")
return nil, errors.WrapCode(err, gidpb.ErrCodeIDGenerateFailed)
}
rsp.Type = "snowflake"
rsp.Id = fmt.Sprintf("%v", da)
case "bigflake":
da, err := id.bigflake.Mint()
if err != nil {
id.log.Err(err).Msg("Failed to generate bigflake id")
err = errors.Wrap(err, "failed to mint bigflake id")
return nil, errors.WrapCode(err, gidpb.ErrCodeIDGenerateFailed)
}
rsp.Type = "bigflake"
rsp.Id = fmt.Sprintf("%v", da)
case "shortid":
da, err := shortid.Generate()
if err != nil {
id.log.Err(err).Msg("Failed to generate shortid id")
err = errors.Wrap(err, "failed to generate short id")
return nil, errors.WrapCode(err, gidpb.ErrCodeIDGenerateFailed)
}
rsp.Type = "shortid"
rsp.Id = da
default:
return nil, errors.WrapCode(errors.New("unsupported id type"), gidpb.ErrCodeIDGenerateFailed)
}
return rsp, nil
}
func (id *Id) Types(ctx context.Context, req *gidpb.TypesRequest) (*gidpb.TypesResponse, error) {
fmt.Println("request-data:", req.String()+"\n\n\n\n")
rsp := new(gidpb.TypesResponse)
rsp.Types = []string{
"uuid",
"shortid",
"snowflake",
"bigflake",
}
return rsp, nil
}