This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.go
91 lines (78 loc) · 2.4 KB
/
interface.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
package rmb
import (
"context"
"encoding/base64"
"fmt"
)
var (
// ErrFunctionNotFound is an err returned if the handler function is not found
ErrFunctionNotFound = fmt.Errorf("function not found")
)
// Handler is a handler function type
type Handler func(ctx context.Context, payload []byte) (interface{}, error)
// Middleware is middleware function type
type Middleware func(ctx context.Context, payload []byte) (context.Context, error)
// Router is the router interface
type Router interface {
WithHandler(route string, handler Handler)
Subroute(route string) Router
Use(Middleware)
}
// Client is an rmb abstract client interface.
type Client interface {
Call(ctx context.Context, twin uint32, fn string, data interface{}, result interface{}) error
}
// Request is an outgoing request struct used to make rpc calls over rmb
type Request struct {
Version int `json:"ver"`
Reference string `json:"ref"`
Command string `json:"cmd"`
Expiration int `json:"exp"`
Data string `json:"dat"`
TwinDest []uint32 `json:"dst"`
RetQueue string `json:"ret"`
Schema string `json:"shm"`
Epoch int64 `json:"now"`
}
// GetPayload returns the payload for a message's data
func (m *Request) GetPayload() ([]byte, error) {
return base64.StdEncoding.DecodeString(m.Data)
}
// Incoming request that need to be handled by servers
type Incoming struct {
Version int `json:"ver"`
Reference string `json:"ref"`
Command string `json:"cmd"`
Expiration int `json:"exp"`
Data string `json:"dat"`
TwinSrc string `json:"src"`
RetQueue string `json:"ret"`
Schema string `json:"shm"`
Epoch int64 `json:"now"`
}
// GetPayload returns the payload for a message's data
func (m *Incoming) GetPayload() ([]byte, error) {
return base64.StdEncoding.DecodeString(m.Data)
}
type Error struct {
Code uint32 `json:"code"`
Message string `json:"message"`
}
type OutgoingResponse struct {
Version int `json:"ver"`
Reference string `json:"ref"`
Data string `json:"dat"`
TwinDest string `json:"dst"`
Schema string `json:"shm"`
Epoch int64 `json:"now"`
Error *Error `json:"err,omitempty"`
}
type IncomingResponse struct {
Version int `json:"ver"`
Reference string `json:"ref"`
Data string `json:"dat"`
TwinSrc string `json:"src"`
Schema string `json:"shm"`
Epoch int64 `json:"now"`
Error *Error `json:"err,omitempty"`
}