2019-01-23 20:15:17 +02:00
|
|
|
// Package micro is a pluggable framework for microservices
|
2016-01-02 21:14:56 +02:00
|
|
|
package micro
|
2015-12-21 01:50:16 +02:00
|
|
|
|
|
|
|
import (
|
2018-03-03 13:53:52 +02:00
|
|
|
"context"
|
|
|
|
|
2024-06-04 22:40:43 +02:00
|
|
|
"go-micro.dev/v5/client"
|
|
|
|
"go-micro.dev/v5/server"
|
2015-12-21 01:50:16 +02:00
|
|
|
)
|
|
|
|
|
2016-01-28 20:23:24 +02:00
|
|
|
type serviceKey struct{}
|
|
|
|
|
2015-12-23 02:02:42 +02:00
|
|
|
// Service is an interface that wraps the lower level libraries
|
|
|
|
// within go-micro. Its a convenience method for building
|
2022-09-30 16:27:07 +02:00
|
|
|
// and initializing services.
|
2015-12-21 01:50:16 +02:00
|
|
|
type Service interface {
|
2019-10-07 09:32:28 +02:00
|
|
|
// The service name
|
|
|
|
Name() string
|
2022-09-30 16:27:07 +02:00
|
|
|
// Init initializes options
|
2016-01-01 03:16:21 +02:00
|
|
|
Init(...Option)
|
2019-10-07 09:32:28 +02:00
|
|
|
// Options returns the current options
|
2016-01-02 21:12:17 +02:00
|
|
|
Options() Options
|
2019-10-07 09:32:28 +02:00
|
|
|
// Client is used to call services
|
2015-12-21 01:50:16 +02:00
|
|
|
Client() client.Client
|
2019-10-07 09:32:28 +02:00
|
|
|
// Server is for handling requests and events
|
2015-12-21 01:50:16 +02:00
|
|
|
Server() server.Server
|
2019-10-07 09:32:28 +02:00
|
|
|
// Run the service
|
2015-12-21 01:50:16 +02:00
|
|
|
Run() error
|
2019-10-07 09:32:28 +02:00
|
|
|
// The service implementation
|
2015-12-21 01:50:16 +02:00
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Event is used to publish messages to a topic.
|
2019-12-18 01:05:46 +02:00
|
|
|
type Event interface {
|
2019-12-16 19:37:11 +02:00
|
|
|
// Publish publishes a message to the event topic
|
2017-03-18 21:00:11 +02:00
|
|
|
Publish(ctx context.Context, msg interface{}, opts ...client.PublishOption) error
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Type alias to satisfy the deprecation.
|
2019-12-18 01:05:46 +02:00
|
|
|
type Publisher = Event
|
|
|
|
|
2015-12-21 01:50:16 +02:00
|
|
|
type Option func(*Options)
|
|
|
|
|
2017-08-25 11:56:57 +02:00
|
|
|
// NewService creates and returns a new Service based on the packages within.
|
2015-12-21 01:50:16 +02:00
|
|
|
func NewService(opts ...Option) Service {
|
|
|
|
return newService(opts...)
|
|
|
|
}
|
2016-01-28 20:23:24 +02:00
|
|
|
|
2016-06-19 17:02:14 +02:00
|
|
|
// FromContext retrieves a Service from the Context.
|
2016-01-28 20:23:24 +02:00
|
|
|
func FromContext(ctx context.Context) (Service, bool) {
|
|
|
|
s, ok := ctx.Value(serviceKey{}).(Service)
|
|
|
|
return s, ok
|
|
|
|
}
|
|
|
|
|
2016-06-19 17:02:14 +02:00
|
|
|
// NewContext returns a new Context with the Service embedded within it.
|
2016-01-28 20:23:24 +02:00
|
|
|
func NewContext(ctx context.Context, s Service) context.Context {
|
|
|
|
return context.WithValue(ctx, serviceKey{}, s)
|
|
|
|
}
|
2017-03-18 21:00:11 +02:00
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// NewEvent creates a new event publisher.
|
2019-12-18 01:05:46 +02:00
|
|
|
func NewEvent(topic string, c client.Client) Event {
|
2017-03-18 21:00:11 +02:00
|
|
|
if c == nil {
|
|
|
|
c = client.NewClient()
|
|
|
|
}
|
2022-10-20 13:00:50 +02:00
|
|
|
|
2019-12-18 01:05:46 +02:00
|
|
|
return &event{c, topic}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// RegisterHandler is syntactic sugar for registering a handler.
|
2017-03-18 21:00:11 +02:00
|
|
|
func RegisterHandler(s server.Server, h interface{}, opts ...server.HandlerOption) error {
|
|
|
|
return s.Handle(s.NewHandler(h, opts...))
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// RegisterSubscriber is syntactic sugar for registering a subscriber.
|
2017-03-18 21:00:11 +02:00
|
|
|
func RegisterSubscriber(topic string, s server.Server, h interface{}, opts ...server.SubscriberOption) error {
|
2017-08-24 09:47:32 +02:00
|
|
|
return s.Subscribe(s.NewSubscriber(topic, h, opts...))
|
2017-03-18 21:00:11 +02:00
|
|
|
}
|