diff --git a/go-micro.go b/go-micro.go new file mode 100644 index 00000000..36dedb0f --- /dev/null +++ b/go-micro.go @@ -0,0 +1,47 @@ +/* +Go micro provides a pluggable library to build microservices. + + import ( + micro "github.com/micro/go-micro" + ) + + service := micro.New( + micro.Registry(r), + micro.Broker(b), + ) + + 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 gomicro + +import ( + "github.com/micro/go-micro/client" + "github.com/micro/go-micro/server" +) + +type Service interface { + Client() client.Client + Server() server.Server + Run() error + String() string +} + +type Option func(*Options) + +var ( + HeaderPrefix = "X-Micro-" + DefaultService = newService() +) + +func NewService(opts ...Option) Service { + return newService(opts...) +} diff --git a/options.go b/options.go new file mode 100644 index 00000000..42864480 --- /dev/null +++ b/options.go @@ -0,0 +1,46 @@ +package gomicro + +import ( + "github.com/micro/go-micro/broker" + "github.com/micro/go-micro/client" + "github.com/micro/go-micro/registry" + "github.com/micro/go-micro/server" + "github.com/micro/go-micro/transport" +) + +type Options struct { + Broker broker.Broker + Client client.Client + Server server.Server + Registry registry.Registry + Transport transport.Transport +} + +func newOptions(opts ...Option) Options { + var opt Options + for _, o := range opts { + o(&opt) + } + + if opt.Broker == nil { + opt.Broker = broker.DefaultBroker + } + + if opt.Client == nil { + opt.Client = client.DefaultClient + } + + if opt.Server == nil { + opt.Server = server.DefaultServer + } + + if opt.Registry == nil { + opt.Registry = registry.DefaultRegistry + } + + if opt.Transport == nil { + opt.Transport = transport.DefaultTransport + } + + return opt +} diff --git a/service.go b/service.go new file mode 100644 index 00000000..f0cc6d49 --- /dev/null +++ b/service.go @@ -0,0 +1,81 @@ +package gomicro + +import ( + "os" + "os/signal" + "syscall" + + "github.com/micro/go-micro/client" + "github.com/micro/go-micro/context" + "github.com/micro/go-micro/server" +) + +type service struct { + opts Options +} + +func newService(opts ...Option) Service { + options := newOptions(opts...) + + options.Client = &clientWrap{ + options.Client, + context.Metadata{ + HeaderPrefix + "From-Service": options.Server.Config().Name(), + }, + } + + return &service{ + opts: options, + } +} + +func (s *service) Client() client.Client { + return s.opts.Client +} + +func (s *service) Server() server.Server { + return s.opts.Server +} + +func (s *service) String() string { + return "go-micro" +} + +func (s *service) Start() error { + if err := s.opts.Server.Start(); err != nil { + return err + } + + if err := s.opts.Server.Register(); err != nil { + return err + } + + return nil +} + +func (s *service) Stop() error { + if err := s.opts.Server.Deregister(); err != nil { + return err + } + + if err := s.opts.Server.Stop(); err != nil { + return err + } + + return nil +} + +func (s *service) Run() error { + if err := s.Start(); err != nil { + return err + } + + ch := make(chan os.Signal, 1) + signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) + + if err := s.Stop(); err != nil { + return err + } + + return nil +} diff --git a/wrap.go b/wrap.go new file mode 100644 index 00000000..671719d7 --- /dev/null +++ b/wrap.go @@ -0,0 +1,28 @@ +package gomicro + +import ( + "github.com/micro/go-micro/client" + cx "github.com/micro/go-micro/context" + + "golang.org/x/net/context" +) + +type clientWrap struct { + client.Client + headers cx.Metadata +} + +func (c *clientWrap) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { + ctx = cx.WithMetadata(ctx, c.headers) + return c.Client.Call(ctx, req, rsp, opts...) +} + +func (c *clientWrap) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Streamer, error) { + ctx = cx.WithMetadata(ctx, c.headers) + return c.Client.Stream(ctx, req, opts...) +} + +func (c *clientWrap) Publish(ctx context.Context, p client.Publication, opts ...client.PublishOption) error { + ctx = cx.WithMetadata(ctx, c.headers) + return c.Client.Publish(ctx, p, opts...) +}