micro/micro.go

86 lines
2.2 KiB
Go
Raw Permalink Normal View History

2019-01-23 21:15:17 +03:00
// Package micro is a pluggable framework for microservices
2016-01-02 22:14:56 +03:00
package micro
2015-12-21 02:50:16 +03:00
import (
2018-03-03 14:53:52 +03:00
"context"
2015-12-21 02:50:16 +03:00
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/server"
)
2016-01-28 21:23:24 +03:00
type serviceKey struct{}
2015-12-23 03:02:42 +03: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-21 02:50:16 +03:00
type Service interface {
2016-01-01 04:16:21 +03:00
Init(...Option)
2016-01-02 22:12:17 +03:00
Options() Options
2015-12-21 02:50:16 +03:00
Client() client.Client
Server() server.Server
Run() error
String() string
}
2017-05-31 17:03:24 +03: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
}
2017-03-18 22:00:11 +03:00
// Publisher is syntactic sugar for publishing
type Publisher interface {
Publish(ctx context.Context, msg interface{}, opts ...client.PublishOption) error
}
2015-12-21 02:50:16 +03:00
type Option func(*Options)
var (
2019-01-24 13:11:02 +03:00
HeaderPrefix = "Micro-"
2015-12-21 02:50:16 +03:00
)
2017-08-25 12:56:57 +03:00
// NewService creates and returns a new Service based on the packages within.
2015-12-21 02:50:16 +03:00
func NewService(opts ...Option) Service {
return newService(opts...)
}
2016-01-28 21:23:24 +03:00
2016-06-19 18:02:14 +03:00
// FromContext retrieves a Service from the Context.
2016-01-28 21:23:24 +03:00
func FromContext(ctx context.Context) (Service, bool) {
s, ok := ctx.Value(serviceKey{}).(Service)
return s, ok
}
2016-06-19 18:02:14 +03:00
// NewContext returns a new Context with the Service embedded within it.
2016-01-28 21:23:24 +03:00
func NewContext(ctx context.Context, s Service) context.Context {
return context.WithValue(ctx, serviceKey{}, s)
}
2017-03-18 22:00:11 +03:00
2017-05-31 22:45:36 +03:00
// NewFunction returns a new Function for a one time executing Service
func NewFunction(opts ...Option) Function {
return newFunction(opts...)
}
2017-03-18 22:00:11 +03:00
// 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 {
2017-08-24 10:47:32 +03:00
return s.Subscribe(s.NewSubscriber(topic, h, opts...))
2017-03-18 22:00:11 +03:00
}