diff --git a/registry/handler/handler.go b/registry/handler/handler.go new file mode 100644 index 00000000..62604e41 --- /dev/null +++ b/registry/handler/handler.go @@ -0,0 +1,70 @@ +package handler + +import ( + "context" + + "github.com/micro/go-micro/errors" + "github.com/micro/go-micro/registry" + pb "github.com/micro/go-micro/registry/proto" + "github.com/micro/go-micro/registry/service" +) + +type Registry struct { + // internal registry + Registry registry.Registry +} + +func (r *Registry) GetService(ctx context.Context, req pb.GetRequest, rsp pb.GetResponse) error { + services, err := r.Registry.GetService(req.Service) + for _, srv := range services { + rsp.Services = append(rsp.Services, service.ToProto(srv)) + } + return nil +} + +func (r *Registry) Register(ctx context.Context, req pb.Service, rsp pb.EmptyResponse) error { + err := r.Registry.Register(service.ToService(req.Service)) + if err != nil { + return errors.InternalServerError("go.micro.registry", err.Error()) + } + return nil +} + +func (r *Registry) Deregister(ctx context.Context, req pb.Service, rsp pb.EmptyResponse) error { + err := r.Registry.Deregister(service.ToService(req.Service)) + if err != nil { + return errors.InternalServerError("go.micro.registry", err.Error()) + } + return nil +} + +func (r *Registry) ListServices(ctx context.Context, req pb.ListRequest, rsp pb.ListResponse) error { + services, err := r.Registry.ListServices() + for _, srv := range services { + rsp.Services = append(rsp.Services, service.ToProto(srv)) + } + return nil +} + +func (r *Registry) Watch(ctx context.Context, req pb.WatchRequest, rsp pb.Registry_WatchStream) error { + watcher, err := r.Registry.Watcher(registry.WatchOption(req.Service)) + if err != nil { + return errors.InternalServerError("go.micro.registry", err.Error()) + } + + for { + next, err := watcher.Next() + if err != nil { + return errors.InternalServerError("go.micro.registry", err.Error()) + } + err = rsp.Send(&pb.Result{ + Action: next.Action, + Service: service.ToProto(next.Service), + }) + if err != nil { + return errors.InternalServerError("go.micro.registry", err.Error()) + } + } + + return nil +} diff --git a/registry/service/service.go b/registry/service/service.go index 796a5283..2473c32f 100644 --- a/registry/service/service.go +++ b/registry/service/service.go @@ -59,7 +59,7 @@ func (s *serviceRegistry) Register(srv *registry.Service, opts ...registry.Regis } // register the service - _, err := s.client.Register(context.TODO(), toProto(srv), s.callOpts()...) + _, err := s.client.Register(context.TODO(), ToProto(srv), s.callOpts()...) if err != nil { return err } @@ -69,7 +69,7 @@ func (s *serviceRegistry) Register(srv *registry.Service, opts ...registry.Regis func (s *serviceRegistry) Deregister(srv *registry.Service) error { // deregister the service - _, err := s.client.Deregister(context.TODO(), toProto(srv), s.callOpts()...) + _, err := s.client.Deregister(context.TODO(), ToProto(srv), s.callOpts()...) if err != nil { return err } @@ -87,7 +87,7 @@ func (s *serviceRegistry) GetService(name string) ([]*registry.Service, error) { var services []*registry.Service for _, service := range rsp.Services { - services = append(services, toService(service)) + services = append(services, ToService(service)) } return services, nil } @@ -100,7 +100,7 @@ func (s *serviceRegistry) ListServices() ([]*registry.Service, error) { var services []*registry.Service for _, service := range rsp.Services { - services = append(services, toService(service)) + services = append(services, ToService(service)) } return services, nil diff --git a/registry/service/util.go b/registry/service/util.go index 3aa7bd40..bbde4095 100644 --- a/registry/service/util.go +++ b/registry/service/util.go @@ -37,7 +37,7 @@ func toValues(v []*pb.Value) []*registry.Value { return vs } -func toProto(s *registry.Service) *pb.Service { +func ToProto(s *registry.Service) *pb.Service { var endpoints []*pb.Endpoint for _, ep := range s.Endpoints { var request, response *pb.Value @@ -85,7 +85,7 @@ func toProto(s *registry.Service) *pb.Service { } } -func toService(s *pb.Service) *registry.Service { +func ToService(s *pb.Service) *registry.Service { var endpoints []*registry.Endpoint for _, ep := range s.Endpoints { var request, response *registry.Value diff --git a/registry/service/watcher.go b/registry/service/watcher.go index c5ede205..08f2144d 100644 --- a/registry/service/watcher.go +++ b/registry/service/watcher.go @@ -26,7 +26,7 @@ func (s *serviceWatcher) Next() (*registry.Result, error) { return ®istry.Result{ Action: r.Action, - Service: toService(r.Service), + Service: ToService(r.Service), }, nil } }