mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-18 08:26:38 +02:00
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
/*
|
|
Go micro provides a pluggable library to build microservices.
|
|
|
|
import (
|
|
micro "github.com/micro/go-micro"
|
|
)
|
|
|
|
service := micro.NewService()
|
|
h := service.Server().NewHandler(&Greeter{})
|
|
service.Server().Handle(h)
|
|
service.Run()
|
|
|
|
|
|
req := service.Client().NewRequest(service, method, request)
|
|
rsp := response{}
|
|
err := service.Client().Call(req, rsp)
|
|
|
|
*/
|
|
|
|
package micro
|
|
|
|
import (
|
|
"github.com/micro/go-micro/client"
|
|
"github.com/micro/go-micro/server"
|
|
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
type serviceKey struct{}
|
|
|
|
// Service is an interface that wraps the lower level libraries
|
|
// within go-micro. Its a convenience method for building
|
|
// and initialising services.
|
|
type Service interface {
|
|
Init(...Option)
|
|
Options() Options
|
|
Client() client.Client
|
|
Server() server.Server
|
|
Run() error
|
|
String() string
|
|
}
|
|
|
|
type Option func(*Options)
|
|
|
|
var (
|
|
HeaderPrefix = "X-Micro-"
|
|
)
|
|
|
|
// NewService creates an returns a new Service based on the packages within.
|
|
func NewService(opts ...Option) Service {
|
|
return newService(opts...)
|
|
}
|
|
|
|
// FromContext retrieves a Service from the Context.
|
|
func FromContext(ctx context.Context) (Service, bool) {
|
|
s, ok := ctx.Value(serviceKey{}).(Service)
|
|
return s, ok
|
|
}
|
|
|
|
// NewContext returns a new Context with the Service embedded within it.
|
|
func NewContext(ctx context.Context, s Service) context.Context {
|
|
return context.WithValue(ctx, serviceKey{}, s)
|
|
}
|