/
server-fuse-grpc.go
129 lines (106 loc) · 2.54 KB
/
server-fuse-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
/*
* Copyright (c) 2022.
* Created by Andy Pangaribuan <https://github.com/apangaribuan>.
* All Rights Reserved.
*/
package server
import (
"context"
"fmt"
"net"
"time"
"github.com/andypangaribuan/project9/f9"
"github.com/andypangaribuan/project9/server/proto/gen/grf"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func (slf *srFuseGrpc) Restful(c context.Context, req *grf.Request) (*grf.Response, error) {
var (
err error
ctx = &srFuseContext{
sendResponse: true,
path: req.Action,
grpcCtx: &srFuseGrpcContext{
ctx: c,
request: req,
header: req.Header,
payload: req.Payload,
params: req.Params,
queries: req.Queries,
},
}
)
ctx.reqCtx = &srFuseContextRequest{
fuseCtx: ctx,
}
handlers, ok := slf.routes[req.Action]
if !ok {
err = fmt.Errorf(`action not found: "%v"`, req.Action)
return ctx.grpcSend(ctx.R500InternalServerError(nil, err))
}
if len(handlers) >= 2 {
err = handlers[0](ctx)
if !ctx.isAuthSet {
return ctx.grpcSend(err)
}
err = handlers[1](ctx)
} else {
err = handlers[0](ctx)
}
return ctx.grpcSend(err)
}
func ConnectFuseGRPC(address string) (ClientFuseGRPC, error) {
fuse := &srConnectFuseGRPC{
address: address,
}
err := fuse.buildConnection()
if err != nil {
return nil, err
}
fuse.buildClient()
return &srClientFuseGRPC{fuseGrpc: fuse}, nil
}
func (slf *srConnectFuseGRPC) buildConnection() error {
if slf.conn != nil {
return nil
}
slf.connMutex.Lock()
defer slf.connMutex.Unlock()
if slf.conn != nil {
return nil
}
conn, err := grpc.Dial(slf.address, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return err
} else {
// make sure we connected to the service
timeout := 5 * time.Second
conn, err := net.DialTimeout("tcp", slf.address, timeout)
if err != nil {
return err
}
_ = conn.Close()
}
slf.conn = conn
return nil
}
func (slf *srConnectFuseGRPC) buildClient() {
if slf.client != nil {
return
}
slf.clientMutex.Lock()
defer slf.clientMutex.Unlock()
slf.client = grf.NewRestfulServiceClient(slf.conn)
}
func (slf *srClientFuseGRPC) Restful(path string, header map[string]string, payload map[string]interface{}, params map[string]string) (*grf.Response, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
request := &grf.Request{
Action: path,
Header: header,
Payload: f9.GetMapStructPbValue(payload),
Params: params,
}
res, err := slf.fuseGrpc.client.Restful(ctx, request)
return res, err
}