2019-01-23 18:15:17 +00:00
|
|
|
// Package micro is a pluggable framework for microservices
|
2016-01-02 19:14:56 +00:00
|
|
|
package micro
|
2015-12-20 23:50:16 +00:00
|
|
|
|
|
|
|
import (
|
2018-03-03 11:53:52 +00:00
|
|
|
"context"
|
|
|
|
|
2015-12-20 23:50:16 +00:00
|
|
|
"github.com/micro/go-micro/client"
|
|
|
|
"github.com/micro/go-micro/server"
|
|
|
|
)
|
|
|
|
|
2016-01-28 18:23:24 +00:00
|
|
|
type serviceKey struct{}
|
|
|
|
|
2015-12-23 00:02:42 +00:00
|
|
|
// Service is an interface that wraps the lower level libraries
|
|
|
|
// within go-micro. Its a convenience method for building
|
|
|
|
// and initialising services.
|
2015-12-20 23:50:16 +00:00
|
|
|
type Service interface {
|
2019-10-07 08:32:28 +01:00
|
|
|
// The service name
|
|
|
|
Name() string
|
|
|
|
// Init initialises options
|
2016-01-01 01:16:21 +00:00
|
|
|
Init(...Option)
|
2019-10-07 08:32:28 +01:00
|
|
|
// Options returns the current options
|
2016-01-02 19:12:17 +00:00
|
|
|
Options() Options
|
2019-10-07 08:32:28 +01:00
|
|
|
// Client is used to call services
|
2015-12-20 23:50:16 +00:00
|
|
|
Client() client.Client
|
2019-10-07 08:32:28 +01:00
|
|
|
// Server is for handling requests and events
|
2015-12-20 23:50:16 +00:00
|
|
|
Server() server.Server
|
2019-10-07 08:32:28 +01:00
|
|
|
// Run the service
|
2015-12-20 23:50:16 +00:00
|
|
|
Run() error
|
2019-10-07 08:32:28 +01:00
|
|
|
// The service implementation
|
2015-12-20 23:50:16 +00:00
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2017-05-31 15:03:24 +01:00
|
|
|
// Function is a one time executing Service
|
|
|
|
type Function interface {
|
|
|
|
// Inherits Service interface
|
|
|
|
Service
|
|
|
|
// Done signals to complete execution
|
|
|
|
Done() error
|
|
|
|
// Handle registers an RPC handler
|
|
|
|
Handle(v interface{}) error
|
|
|
|
// Subscribe registers a subscriber
|
|
|
|
Subscribe(topic string, v interface{}) error
|
|
|
|
}
|
|
|
|
|
2019-12-16 17:37:11 +00:00
|
|
|
/*
|
|
|
|
// Type Event is a future type for acting on asynchronous events
|
|
|
|
type Event interface {
|
|
|
|
// Publish publishes a message to the event topic
|
|
|
|
Publish(ctx context.Context, msg interface{}, opts ...client.PublishOption) error
|
|
|
|
// Subscribe to the event
|
|
|
|
Subscribe(ctx context.Context, v in
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resource is a future type for defining dependencies
|
|
|
|
type Resource interface {
|
|
|
|
// Name of the resource
|
|
|
|
Name() string
|
|
|
|
// Type of resource
|
|
|
|
Type() string
|
|
|
|
// Method of creation
|
|
|
|
Create() error
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2019-12-17 23:05:46 +00:00
|
|
|
// Event is used to publish messages to a topic
|
|
|
|
type Event interface {
|
2019-12-16 17:37:11 +00:00
|
|
|
// Publish publishes a message to the event topic
|
2017-03-18 19:00:11 +00:00
|
|
|
Publish(ctx context.Context, msg interface{}, opts ...client.PublishOption) error
|
|
|
|
}
|
|
|
|
|
2019-12-17 23:05:46 +00:00
|
|
|
// Type alias to satisfy the deprecation
|
|
|
|
type Publisher = Event
|
|
|
|
|
2015-12-20 23:50:16 +00:00
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
var (
|
2019-01-24 10:11:02 +00:00
|
|
|
HeaderPrefix = "Micro-"
|
2015-12-20 23:50:16 +00:00
|
|
|
)
|
|
|
|
|
2017-08-25 17:56:57 +08:00
|
|
|
// NewService creates and returns a new Service based on the packages within.
|
2015-12-20 23:50:16 +00:00
|
|
|
func NewService(opts ...Option) Service {
|
|
|
|
return newService(opts...)
|
|
|
|
}
|
2016-01-28 18:23:24 +00:00
|
|
|
|
2016-06-19 16:02:14 +01:00
|
|
|
// FromContext retrieves a Service from the Context.
|
2016-01-28 18:23:24 +00:00
|
|
|
func FromContext(ctx context.Context) (Service, bool) {
|
|
|
|
s, ok := ctx.Value(serviceKey{}).(Service)
|
|
|
|
return s, ok
|
|
|
|
}
|
|
|
|
|
2016-06-19 16:02:14 +01:00
|
|
|
// NewContext returns a new Context with the Service embedded within it.
|
2016-01-28 18:23:24 +00:00
|
|
|
func NewContext(ctx context.Context, s Service) context.Context {
|
|
|
|
return context.WithValue(ctx, serviceKey{}, s)
|
|
|
|
}
|
2017-03-18 19:00:11 +00:00
|
|
|
|
2017-05-31 20:45:36 +01:00
|
|
|
// NewFunction returns a new Function for a one time executing Service
|
|
|
|
func NewFunction(opts ...Option) Function {
|
|
|
|
return newFunction(opts...)
|
|
|
|
}
|
|
|
|
|
2019-12-17 23:05:46 +00:00
|
|
|
// NewEvent creates a new event publisher
|
|
|
|
func NewEvent(topic string, c client.Client) Event {
|
2017-03-18 19:00:11 +00:00
|
|
|
if c == nil {
|
|
|
|
c = client.NewClient()
|
|
|
|
}
|
2019-12-17 23:05:46 +00:00
|
|
|
return &event{c, topic}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated: NewPublisher returns a new Publisher
|
|
|
|
func NewPublisher(topic string, c client.Client) Event {
|
|
|
|
return NewEvent(topic, c)
|
2017-03-18 19:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2017-08-24 10:47:32 +03:00
|
|
|
return s.Subscribe(s.NewSubscriber(topic, h, opts...))
|
2017-03-18 19:00:11 +00:00
|
|
|
}
|