1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00
go-micro/go-micro.go

46 lines
1.1 KiB
Go
Raw Normal View History

2016-11-20 16:59:09 +02:00
// Package micro is a pluggable RPC framework for microservices
2016-01-02 21:14:56 +02:00
package micro
2015-12-21 01:50:16 +02:00
import (
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/server"
2016-01-28 20:23:24 +02:00
"golang.org/x/net/context"
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
// and initialising services.
2015-12-21 01:50:16 +02:00
type Service interface {
2016-01-01 03:16:21 +02:00
Init(...Option)
2016-01-02 21:12:17 +02:00
Options() Options
2015-12-21 01:50:16 +02:00
Client() client.Client
Server() server.Server
Run() error
String() string
}
type Option func(*Options)
var (
2015-12-21 03:12:29 +02:00
HeaderPrefix = "X-Micro-"
2015-12-21 01:50:16 +02:00
)
2016-06-19 17:02:14 +02:00
// NewService creates an 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)
}