1
- // Package broker provides a http based message broker
2
- package broker
1
+ // Package http provides a http based message broker
2
+ package http
3
3
4
4
import (
5
5
"bytes"
@@ -16,6 +16,7 @@ import (
16
16
"time"
17
17
18
18
"github.com/google/uuid"
19
+ "go-micro.dev/v5/broker"
19
20
"go-micro.dev/v5/codec/json"
20
21
merr "go-micro.dev/v5/errors"
21
22
"go-micro.dev/v5/registry"
@@ -29,7 +30,7 @@ import (
29
30
30
31
// HTTP Broker is a point to point async broker.
31
32
type httpBroker struct {
32
- opts Options
33
+ opts broker. Options
33
34
34
35
r registry.Registry
35
36
@@ -51,8 +52,8 @@ type httpBroker struct {
51
52
}
52
53
53
54
type httpSubscriber struct {
54
- opts SubscribeOptions
55
- fn Handler
55
+ opts broker. SubscribeOptions
56
+ fn broker. Handler
56
57
svc * registry.Service
57
58
hb * httpBroker
58
59
id string
@@ -61,7 +62,7 @@ type httpSubscriber struct {
61
62
62
63
type httpEvent struct {
63
64
err error
64
- m * Message
65
+ m * broker. Message
65
66
t string
66
67
}
67
68
@@ -108,8 +109,8 @@ func newTransport(config *tls.Config) *http.Transport {
108
109
return t
109
110
}
110
111
111
- func newHttpBroker (opts ... Option ) Broker {
112
- options := * NewOptions (opts ... )
112
+ func newHttpBroker (opts ... broker. Option ) broker. Broker {
113
+ options := * broker . NewOptions (opts ... )
113
114
114
115
options .Registry = registry .DefaultRegistry
115
116
options .Codec = json.Marshaler {}
@@ -161,15 +162,15 @@ func (h *httpEvent) Error() error {
161
162
return h .err
162
163
}
163
164
164
- func (h * httpEvent ) Message () * Message {
165
+ func (h * httpEvent ) Message () * broker. Message {
165
166
return h .m
166
167
}
167
168
168
169
func (h * httpEvent ) Topic () string {
169
170
return h .t
170
171
}
171
172
172
- func (h * httpSubscriber ) Options () SubscribeOptions {
173
+ func (h * httpSubscriber ) Options () broker. SubscribeOptions {
173
174
return h .opts
174
175
}
175
176
@@ -308,7 +309,7 @@ func (h *httpBroker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
308
309
return
309
310
}
310
311
311
- var m * Message
312
+ var m * broker. Message
312
313
if err = h .opts .Codec .Unmarshal (b , & m ); err != nil {
313
314
errr := merr .InternalServerError ("go.micro.broker" , "Error parsing request body: %v" , err )
314
315
w .WriteHeader (500 )
@@ -330,7 +331,7 @@ func (h *httpBroker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
330
331
id := req .Form .Get ("id" )
331
332
332
333
//nolint:prealloc
333
- var subs []Handler
334
+ var subs []broker. Handler
334
335
335
336
h .RLock ()
336
337
for _ , subscriber := range h .subscribers [topic ] {
@@ -458,7 +459,7 @@ func (h *httpBroker) Disconnect() error {
458
459
return err
459
460
}
460
461
461
- func (h * httpBroker ) Init (opts ... Option ) error {
462
+ func (h * httpBroker ) Init (opts ... broker. Option ) error {
462
463
h .RLock ()
463
464
if h .running {
464
465
h .RUnlock ()
@@ -505,13 +506,13 @@ func (h *httpBroker) Init(opts ...Option) error {
505
506
return nil
506
507
}
507
508
508
- func (h * httpBroker ) Options () Options {
509
+ func (h * httpBroker ) Options () broker. Options {
509
510
return h .opts
510
511
}
511
512
512
- func (h * httpBroker ) Publish (topic string , msg * Message , opts ... PublishOption ) error {
513
+ func (h * httpBroker ) Publish (topic string , msg * broker. Message , opts ... broker .PublishOption ) error {
513
514
// create the message first
514
- m := & Message {
515
+ m := & broker. Message {
515
516
Header : make (map [string ]string ),
516
517
Body : msg .Body ,
517
518
}
@@ -637,10 +638,10 @@ func (h *httpBroker) Publish(topic string, msg *Message, opts ...PublishOption)
637
638
return nil
638
639
}
639
640
640
- func (h * httpBroker ) Subscribe (topic string , handler Handler , opts ... SubscribeOption ) (Subscriber , error ) {
641
+ func (h * httpBroker ) Subscribe (topic string , handler broker. Handler , opts ... broker. SubscribeOption ) (broker. Subscriber , error ) {
641
642
var err error
642
643
var host , port string
643
- options := NewSubscribeOptions (opts ... )
644
+ options := broker . NewSubscribeOptions (opts ... )
644
645
645
646
// parse address for host, port
646
647
host , port , err = net .SplitHostPort (h .Address ())
@@ -705,7 +706,7 @@ func (h *httpBroker) String() string {
705
706
return "http"
706
707
}
707
708
708
- // NewBroker returns a new http broker.
709
- func NewBroker (opts ... Option ) Broker {
709
+ // NewHttpBroker returns a new http broker.
710
+ func NewHttpBroker (opts ... broker. Option ) broker. Broker {
710
711
return newHttpBroker (opts ... )
711
712
}
0 commit comments