forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
52 lines (42 loc) · 1.95 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
43
44
45
46
47
48
49
50
51
52
package courier
import (
"context"
"net/http"
"github.com/nyaruka/gocommon/urns"
)
// Event is our interface for the types of things a ChannelHandleFunc can return.
type Event interface {
EventID() int64
}
// ChannelHandleFunc is the interface ChannelHandlers must satisfy to handle incoming requests.
// The Server will take care of looking up the channel by UUID before passing it to this function.
// Errors in format of the request or by the caller should be handled and logged internally. Errors in
// execution or in courier itself should be passed back.
type ChannelHandleFunc func(context.Context, Channel, http.ResponseWriter, *http.Request) ([]Event, error)
// ChannelHandler is the interface all handlers must satisfy
type ChannelHandler interface {
Initialize(Server) error
ChannelType() ChannelType
ChannelName() string
UseChannelRouteUUID() bool
GetChannel(context.Context, *http.Request) (Channel, error)
SendMsg(context.Context, Msg) (MsgStatus, error)
}
// URNDescriber is the interface handlers which can look up URN metadata for new contacts should satisfy.
type URNDescriber interface {
DescribeURN(context.Context, Channel, urns.URN) (map[string]string, error)
}
// MediaDownloadRequestBuilder is the interface handlers which can allow a custom way to download attachment media for messages should satisfy
type MediaDownloadRequestBuilder interface {
BuildDownloadMediaRequest(context.Context, Backend, Channel, string) (*http.Request, 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
}
// GetHandler returns the handler for the passed in channel type, or nil if not found
func GetHandler(ct ChannelType) ChannelHandler {
return registeredHandlers[ct]
}
var registeredHandlers = make(map[ChannelType]ChannelHandler)
var activeHandlers = make(map[ChannelType]ChannelHandler)