-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (64 loc) · 1.78 KB
/
main.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
package jsonrpc
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/gumeniukcom/golang-jsonrpc2/structs"
)
//JSONRPC container for jsonrpc
type JSONRPC struct {
errors ErrorMessages
methods RPCMethods
globalInterceptors InterceptorCallMethods
defaultTimeOut time.Duration
}
// New creates new instance of JSONRPC
func New() *JSONRPC {
return &JSONRPC{
errors: ErrorMessages{
ParseErrorCode: "parse_error_not_well_formed",
InvalidRequestErrorCode: "invalid_request_not_conforming_to_spec",
MethodNotFoundErrorCode: "requested_method_not_found",
InvalidParamsErrorCode: "invalid_method_parameters",
InternalErrorCode: "internal_error",
MethodNotImplemented: "method_not_implemented",
RequestTimeLimit: "request_time_limit",
},
methods: RPCMethods{},
globalInterceptors: InterceptorCallMethods{},
defaultTimeOut: 30,
}
}
//SetDefaultTimeOut set timeout for func run
func (j *JSONRPC) SetDefaultTimeOut(timeout int) {
j.defaultTimeOut = time.Duration(timeout)
}
// call method for call function from registry
// TODO: think about validate income data
func (j *JSONRPC) call(
ctx context.Context,
methodName string,
data json.RawMessage,
id interface{},
) (resp *structs.Response) {
defer func() {
if r := recover(); r != nil {
resp = j.Error(ctx, fmt.Errorf("%#v", r), InternalErrorCode, id)
}
}()
ctx, code, err := j.callGlobalInterceptors(ctx, methodName, data, id)
if err != nil {
return j.Error(ctx, err, code, id)
}
method, ok := j.methods[methodName]
if !ok {
return j.Error(ctx, nil, MethodNotFoundErrorCode, id)
}
res, errCode, err := method(ctx, data)
if err != nil {
return j.Error(ctx, err, errCode, id)
}
resp = Response(ctx, id, &res, nil)
return resp
}