forked from xen0n/go-workwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rx.go
60 lines (46 loc) · 1.24 KB
/
rx.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
package workwx
import (
"net/http"
"github.com/xen0n/go-workwx/internal/lowlevel/envelope"
"github.com/xen0n/go-workwx/internal/lowlevel/httpapi"
)
// RxMessageHandler 用来接收消息的接口。
type RxMessageHandler interface {
// OnIncomingMessage 一条消息到来时的回调。
OnIncomingMessage(msg *RxMessage) error
}
type lowlevelEnvelopeHandler struct {
highlevelHandler RxMessageHandler
}
var _ httpapi.EnvelopeHandler = (*lowlevelEnvelopeHandler)(nil)
func (h *lowlevelEnvelopeHandler) OnIncomingEnvelope(rx envelope.Envelope) error {
msg, err := fromEnvelope(rx.Msg)
if err != nil {
return err
}
return h.highlevelHandler.OnIncomingMessage(msg)
}
type HTTPHandler struct {
inner *httpapi.LowlevelHandler
}
var _ http.Handler = (*HTTPHandler)(nil)
func NewHTTPHandler(
token string,
encodingAESKey string,
rxMessageHandler RxMessageHandler,
) (*HTTPHandler, error) {
lleh := &lowlevelEnvelopeHandler{
highlevelHandler: rxMessageHandler,
}
llHandler, err := httpapi.NewLowlevelHandler(token, encodingAESKey, lleh)
if err != nil {
return nil, err
}
obj := HTTPHandler{
inner: llHandler,
}
return &obj, nil
}
func (h *HTTPHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
h.inner.ServeHTTP(rw, r)
}