2024-10-19 14:36:33 +00:00
|
|
|
package directory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-10-20 07:22:19 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2024-10-19 14:36:33 +00:00
|
|
|
|
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/samber/lo"
|
|
|
|
)
|
|
|
|
|
2024-10-20 07:22:19 +00:00
|
|
|
type ServiceRpcServer struct {
|
2024-10-21 14:07:36 +00:00
|
|
|
proto.UnimplementedDirectoryServiceServer
|
2024-10-19 14:36:33 +00:00
|
|
|
}
|
|
|
|
|
2024-10-20 07:22:19 +00:00
|
|
|
func instantiationService(in *ServiceInstance) *proto.ServiceInfo {
|
2024-10-19 14:36:33 +00:00
|
|
|
if in == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &proto.ServiceInfo{
|
|
|
|
Id: in.ID,
|
|
|
|
Type: in.Type,
|
|
|
|
Label: in.Label,
|
|
|
|
GrpcAddr: in.GrpcAddr,
|
|
|
|
HttpAddr: in.HttpAddr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-20 07:22:19 +00:00
|
|
|
func (v *ServiceRpcServer) GetService(ctx context.Context, request *proto.GetServiceRequest) (*proto.GetServiceResponse, error) {
|
2024-10-19 14:36:33 +00:00
|
|
|
if request.Id != nil {
|
|
|
|
out := GetServiceInstance(request.GetId())
|
|
|
|
return &proto.GetServiceResponse{
|
2024-10-20 07:22:19 +00:00
|
|
|
Data: instantiationService(out),
|
2024-10-19 14:36:33 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
if request.Type != nil {
|
|
|
|
out := GetServiceInstanceByType(request.GetType())
|
|
|
|
return &proto.GetServiceResponse{
|
2024-10-20 07:22:19 +00:00
|
|
|
Data: instantiationService(out),
|
2024-10-19 14:36:33 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("no filter condition is provided")
|
|
|
|
}
|
|
|
|
|
2024-10-20 07:22:19 +00:00
|
|
|
func (v *ServiceRpcServer) ListService(ctx context.Context, request *proto.ListServiceRequest) (*proto.ListServiceResponse, error) {
|
2024-10-19 14:36:33 +00:00
|
|
|
var out []*ServiceInstance
|
|
|
|
if request.Type != nil {
|
|
|
|
out = ListServiceInstanceByType(request.GetType())
|
|
|
|
} else {
|
|
|
|
out = ListServiceInstance()
|
|
|
|
}
|
|
|
|
return &proto.ListServiceResponse{
|
|
|
|
Data: lo.Map(out, func(item *ServiceInstance, index int) *proto.ServiceInfo {
|
2024-10-20 07:22:19 +00:00
|
|
|
return instantiationService(item)
|
2024-10-19 14:36:33 +00:00
|
|
|
}),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-10-20 07:22:19 +00:00
|
|
|
func (v *ServiceRpcServer) AddService(ctx context.Context, info *proto.ServiceInfo) (*proto.AddServiceResponse, error) {
|
|
|
|
clientId, err := GetClientId(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.GetId() != clientId {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "client_id mismatch in metadata")
|
2024-10-24 14:40:14 +00:00
|
|
|
} else if len(clientId) == 0 {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "client_id must not be blank")
|
2024-10-20 07:22:19 +00:00
|
|
|
}
|
|
|
|
|
2024-10-19 14:36:33 +00:00
|
|
|
in := &ServiceInstance{
|
2024-10-20 07:22:19 +00:00
|
|
|
ID: clientId,
|
2024-10-19 14:36:33 +00:00
|
|
|
Type: info.GetType(),
|
|
|
|
Label: info.GetLabel(),
|
|
|
|
GrpcAddr: info.GetGrpcAddr(),
|
|
|
|
HttpAddr: info.HttpAddr,
|
|
|
|
}
|
2024-10-21 14:47:31 +00:00
|
|
|
err = AddServiceInstance(in)
|
|
|
|
if err == nil {
|
|
|
|
log.Info().Str("id", clientId).Str("label", info.GetLabel()).Msg("New service registered")
|
|
|
|
} else {
|
|
|
|
log.Error().Str("id", clientId).Str("label", info.GetLabel()).Err(err).Msg("Unable to register a service")
|
|
|
|
}
|
2024-10-19 14:36:33 +00:00
|
|
|
return &proto.AddServiceResponse{
|
2024-10-21 14:47:31 +00:00
|
|
|
IsSuccess: err == nil,
|
2024-10-19 14:36:33 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-10-20 07:22:19 +00:00
|
|
|
func (v *ServiceRpcServer) RemoveService(ctx context.Context, request *proto.RemoveServiceRequest) (*proto.RemoveServiceResponse, error) {
|
2024-10-21 14:47:31 +00:00
|
|
|
err := RemoveServiceInstance(request.GetId())
|
|
|
|
if err == nil {
|
|
|
|
log.Info().Str("id", request.GetId()).Msg("A service removed")
|
|
|
|
} else {
|
|
|
|
log.Error().Str("id", request.GetId()).Err(err).Msg("Unable to remove a service")
|
|
|
|
}
|
2024-10-19 14:36:33 +00:00
|
|
|
return &proto.RemoveServiceResponse{
|
2024-10-21 14:47:31 +00:00
|
|
|
IsSuccess: err == nil,
|
2024-10-19 14:36:33 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2024-10-27 04:54:39 +00:00
|
|
|
|
|
|
|
func (v *ServiceRpcServer) BroadcastEvent(ctx context.Context, in *proto.EventInfo) (*proto.EventResponse, error) {
|
|
|
|
services := ListServiceInstance()
|
2024-11-23 04:18:35 +00:00
|
|
|
log.Debug().
|
|
|
|
Int("destinations", len(services)).
|
|
|
|
Str("event", in.GetEvent()).
|
|
|
|
Msg("Broadcasting event...")
|
2024-10-27 04:54:39 +00:00
|
|
|
for _, service := range services {
|
|
|
|
conn, err := service.GetGrpcConn()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, _ = proto.NewDirectoryServiceClient(conn).BroadcastEvent(ctx, in)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.EventResponse{}, nil
|
|
|
|
}
|