micro/registry/handler/handler.go
2019-09-09 09:20:17 -07:00

71 lines
1.9 KiB
Go

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
}