-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
61 lines (52 loc) · 1.3 KB
/
option.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
package mx
import (
"context"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
)
type RegisterOption struct {
Method string
Conn *grpc.ClientConn
Impl interface{}
Client interface{}
ClientCtor ClientCtor
ServiceHandleClient ServiceHandleClient
Handler ConnServiceHandler
}
type (
ClientCtor any
ServiceHandleClient any
)
type (
RegisterOptFunc func(*RegisterOption) error
ConnServiceHandler func(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error
)
const (
// DefaultServicePattern default service pattern
ServiceMethodImpl = "impl"
ServiceMethodHandler = "handler"
ServiceMethodClient = "client"
ServiceMethodConn = "conn"
)
func WithConnString(connString string) RegisterOptFunc {
return func(o *RegisterOption) (err error) {
o.Conn, err = grpc.Dial(connString, grpc.WithInsecure())
if err != nil {
return err
}
return nil
}
}
func WithConn(conn *grpc.ClientConn) RegisterOptFunc {
return func(o *RegisterOption) error {
o.Conn = conn
return nil
}
}
func WithServiceHandler(handler ConnServiceHandler) RegisterOptFunc {
return func(o *RegisterOption) error {
o.Method = ServiceMethodHandler
o.Handler = handler
return nil
}
}