mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-24 08:02:32 +02:00
Syntactic sugar for pubsub
This commit is contained in:
parent
0c2c53e9af
commit
d226bdf2d4
23
go-micro.go
23
go-micro.go
@ -22,6 +22,11 @@ type Service interface {
|
|||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Publisher is syntactic sugar for publishing
|
||||||
|
type Publisher interface {
|
||||||
|
Publish(ctx context.Context, msg interface{}, opts ...client.PublishOption) error
|
||||||
|
}
|
||||||
|
|
||||||
type Option func(*Options)
|
type Option func(*Options)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -43,3 +48,21 @@ func FromContext(ctx context.Context) (Service, bool) {
|
|||||||
func NewContext(ctx context.Context, s Service) context.Context {
|
func NewContext(ctx context.Context, s Service) context.Context {
|
||||||
return context.WithValue(ctx, serviceKey{}, s)
|
return context.WithValue(ctx, serviceKey{}, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewPublisher returns a new Publisher
|
||||||
|
func NewPublisher(topic string, c client.Client) Publisher {
|
||||||
|
if c == nil {
|
||||||
|
c = client.NewClient()
|
||||||
|
}
|
||||||
|
return &publisher{c, topic}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterHandler is syntactic sugar for registering a handler
|
||||||
|
func RegisterHandler(s server.Server, h interface{}, opts ...server.HandlerOption) error {
|
||||||
|
return s.Handle(s.NewHandler(h, opts...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterSubscriber is syntactic sugar for registering a subscriber
|
||||||
|
func RegisterSubscriber(topic string, s server.Server, h interface{}, opts ...server.SubscriberOption) error {
|
||||||
|
return s.Subscribe(s.NewSubscriber(topic, h))
|
||||||
|
}
|
||||||
|
15
publisher.go
Normal file
15
publisher.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package micro
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/micro/go-micro/client"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type publisher struct {
|
||||||
|
c client.Client
|
||||||
|
topic string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *publisher) Publish(ctx context.Context, msg interface{}, opts ...client.PublishOption) error {
|
||||||
|
return p.c.Publish(ctx, p.c.NewPublication(p.topic, msg))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user