micro/go-micro.go

64 lines
1.4 KiB
Go
Raw Normal View History

2015-12-21 02:50:16 +03:00
/*
Go micro provides a pluggable library to build microservices.
import (
micro "github.com/micro/go-micro"
)
2015-12-21 02:52:58 +03:00
service := micro.NewService()
2015-12-21 02:50:16 +03:00
h := service.Server().NewHandler(&Greeter{})
service.Server().Handle(h)
service.Run()
2015-12-21 02:52:58 +03:00
req := service.Client().NewRequest(service, method, request)
2015-12-21 02:50:16 +03:00
rsp := response{}
2015-12-21 02:52:58 +03:00
err := service.Client().Call(req, rsp)
2015-12-21 02:50:16 +03:00
*/
2016-01-02 22:14:56 +03:00
package micro
2015-12-21 02:50:16 +03:00
import (
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/server"
2016-01-28 21:23:24 +03:00
"golang.org/x/net/context"
2015-12-21 02:50:16 +03:00
)
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
}
type Option func(*Options)
var (
2015-12-21 04:12:29 +03:00
HeaderPrefix = "X-Micro-"
2015-12-21 02:50:16 +03:00
)
2016-06-19 18:02:14 +03:00
// NewService creates an 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)
}