forked from xen0n/go-workwx
/
event_api.go
44 lines (37 loc) · 1002 Bytes
/
event_api.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
package httpapi
import (
"io/ioutil"
"net/http"
"github.com/61qt/go-workwx/internal/lowlevel/envelope"
)
type EnvelopeHandler interface {
OnIncomingEnvelope(rx envelope.Envelope) error
}
func (h *LowlevelHandler) eventHandler(
rw http.ResponseWriter,
r *http.Request,
) {
// request bodies are assumed small
// we can't do streaming parse/decrypt/verification anyway
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
// signature verification is inside EnvelopeProcessor
ev, err := h.ep.HandleIncomingMsg(r.URL, body)
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
return
}
err = h.eh.OnIncomingEnvelope(ev)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
// currently we always return empty 200 responses
// any reply is to be sent asynchronously
// this might change in the future (maybe save a couple of RTT or so)
rw.WriteHeader(http.StatusOK)
}