-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_funcmapper.go
75 lines (63 loc) · 1.96 KB
/
server_funcmapper.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
package grpcp
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/ambientkit/ambient"
"github.com/ambientkit/ambient/pkg/grpcp/protodef"
"golang.org/x/net/context"
"google.golang.org/protobuf/types/known/anypb"
)
// GRPCFuncMapperServer handles server calls for FuncMap.
type GRPCFuncMapperServer struct {
client protodef.FuncMapperClient
Log ambient.Logger
}
// Do handler.
func (l *GRPCFuncMapperServer) Do(r *http.Request, requestID string, key string, args []interface{}, globalFuncMap bool) (interface{}, string, error) {
var err error
ctx := context.Background()
arr := make([]*anypb.Any, len(args))
for i, v := range args {
arr[i], err = InterfaceToProtobufAny(v)
if err != nil {
return nil, "", fmt.Errorf("Do args error: %v", err.Error())
}
}
sm, err := ObjectToProtobufStruct(r.Header)
if err != nil {
return nil, "", fmt.Errorf("Do header conversion error: %v", err.Error())
}
body := bytes.NewBuffer(nil)
_, err = io.Copy(body, r.Body)
if err != nil {
return nil, "", fmt.Errorf("Do body copy error: %v", err.Error())
}
// Restore body.
r.Body = ioutil.NopCloser(body)
resp, err := l.client.Do(ctx, &protodef.FuncMapperDoRequest{
Globalfm: globalFuncMap,
Key: key,
Requestid: requestID,
Params: arr,
Method: r.Method,
Path: r.RequestURI,
Headers: sm,
Body: body.Bytes(),
})
if err != nil {
return nil, "", fmt.Errorf("Do response error:%v", err.Error())
}
//l.Log.Error("SERVER: Kind: %v | Value: %v | Valid: %v | Args: %#v", reflect.TypeOf(resp.Value).Kind(), resp.Value, reflect.ValueOf(resp.Value).IsValid(), args)
var i interface{}
if resp.Value != nil {
err = ProtobufAnyToInterface(resp.Value, &i)
} else {
err = ProtobufStructToArray(resp.Arr, &i)
}
//l.Log.Error("SERVED: Kind: %v | Value: %v | Valid: %v", reflect.TypeOf(i).Kind(), i, reflect.ValueOf(i).IsValid())
//l.Log.Error("SERVED: Type: %v | Value: %v", reflect.TypeOf(i), i)
return i, resp.Error, err
}