forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
42 lines (34 loc) · 1.79 KB
/
handler.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
package courier
import (
"net/http"
)
// ReceiveEvent is our interface for the types of things a ChannelReceiveMsgFunc can return. Currently
// Msgs and ChannelEvent both satisfy this
type ReceiveEvent interface {
ReceiveID() int64
}
// ChannelReceiveMsgFunc is the interface ChannelHandler functions must satisfy to handle incoming msgs
// The Server will take care of looking up the channel by UUID before passing it to this function.
type ChannelReceiveMsgFunc func(Channel, http.ResponseWriter, *http.Request) ([]ReceiveEvent, error)
// ChannelUpdateStatusFunc is the interface ChannelHandler functions must satisfy to handle incoming
// status requests. The Server will take care of looking up the channel by UUID before passing it to this function.
type ChannelUpdateStatusFunc func(Channel, http.ResponseWriter, *http.Request) ([]MsgStatus, error)
// ChannelActionHandlerFunc is the interface ChannelHandler functions must satisfy to handle other types
// of requests. These generic handlers should only be used if they are not dealing with receiving messages
// or status updates.
//
// The Server will take care of looking up the channel by UUID before passing it to this function.
type ChannelActionHandlerFunc func(Channel, http.ResponseWriter, *http.Request) error
// ChannelHandler is the interface all handlers must satisfy
type ChannelHandler interface {
Initialize(Server) error
ChannelType() ChannelType
ChannelName() string
SendMsg(Msg) (MsgStatus, error)
}
// RegisterHandler adds a new handler for a channel type, this is called by individual handlers when they are initialized
func RegisterHandler(handler ChannelHandler) {
registeredHandlers[handler.ChannelType()] = handler
}
var registeredHandlers = make(map[ChannelType]ChannelHandler)
var activeHandlers = make(map[ChannelType]ChannelHandler)