-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
43 lines (33 loc) · 847 Bytes
/
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
package handler
import (
"context"
"github.com/alexfalkowski/go-service/transport/nsq/marshaller"
"github.com/alexfalkowski/go-service/transport/nsq/message"
"github.com/nsqio/go-nsq"
)
// Handler for NSQ.
type Handler interface {
Handle(ctx context.Context, message *message.Message) error
}
// Params for handler.
type Params struct {
Handler Handler
Marshaller marshaller.Marshaller
}
// New handler for NSQ.
func New(params Params) nsq.Handler {
return &handler{Marshaller: params.Marshaller, Handler: params.Handler}
}
type handler struct {
Marshaller marshaller.Marshaller
Handler
}
func (h *handler) HandleMessage(m *nsq.Message) error {
var msg message.Message
if err := h.Marshaller.Unmarshal(m.Body, &msg); err != nil {
return err
}
msg.Message = m
ctx := context.Background()
return h.Handle(ctx, &msg)
}