mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-24 08:02:32 +02:00
Add registry handler
This commit is contained in:
parent
b076ef906a
commit
1f44d7a4a1
70
registry/handler/handler.go
Normal file
70
registry/handler/handler.go
Normal file
@ -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
|
||||
}
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user