2022-08-11 15:21:32 +00:00
|
|
|
package grpcapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
context "context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"git.coopgo.io/coopgo-platform/groups-management/handlers"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
codes "google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/reflection"
|
|
|
|
status "google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GroupsManagementServerImpl struct {
|
|
|
|
handler handlers.GroupsManagementHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGroupsManagementServer(h handlers.GroupsManagementHandler) *GroupsManagementServerImpl {
|
|
|
|
return &GroupsManagementServerImpl{
|
|
|
|
handler: h,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s GroupsManagementServerImpl) AddGroup(ctx context.Context, req *AddGroupRequest) (*AddGroupResponse, error) {
|
|
|
|
|
|
|
|
g := req.Group.ToStorageType()
|
|
|
|
group, err := s.handler.AddGroup(g)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.AlreadyExists, "group creation failed : %v", err)
|
|
|
|
}
|
|
|
|
response, err := GroupFromStorageType(group)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.Internal, "issue while retrieving group : %v", err)
|
|
|
|
}
|
|
|
|
return &AddGroupResponse{Group: response}, nil
|
|
|
|
}
|
|
|
|
func (s GroupsManagementServerImpl) GetGroup(ctx context.Context, req *GetGroupRequest) (*GetGroupResponse, error) {
|
|
|
|
group, err := s.handler.GetGroup(req.Id)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving group : %v", err)
|
|
|
|
}
|
|
|
|
response, err := GroupFromStorageType(group)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.Internal, "issue while retrieving group : %v", err)
|
|
|
|
}
|
|
|
|
return &GetGroupResponse{Group: response}, nil
|
|
|
|
}
|
|
|
|
func (s GroupsManagementServerImpl) GetGroups(ctx context.Context, req *GetGroupsRequest) (*GetGroupsResponse, error) {
|
2022-12-05 16:18:01 +00:00
|
|
|
responses, err := s.handler.GetGroups(req.Namespaces, req.Member)
|
2022-08-11 15:21:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "could not get groups : %v", err)
|
|
|
|
}
|
|
|
|
var groups []*Group
|
|
|
|
|
|
|
|
for _, g := range responses {
|
|
|
|
group, err := GroupFromStorageType(&g)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "could not get groups : %v", err)
|
|
|
|
}
|
|
|
|
groups = append(groups, group)
|
|
|
|
}
|
|
|
|
return &GetGroupsResponse{Groups: groups}, nil
|
|
|
|
}
|
|
|
|
func (s GroupsManagementServerImpl) GetGroupsBatch(ctx context.Context, req *GetGroupsBatchRequest) (*GetGroupsBatchResponse, error) {
|
|
|
|
responses, err := s.handler.GetGroupsBatch(req.Groupids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "could not get groups : %v", err)
|
|
|
|
}
|
|
|
|
var groups []*Group
|
|
|
|
|
|
|
|
for _, g := range responses {
|
|
|
|
group, err := GroupFromStorageType(&g)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "could not get groups : %v", err)
|
|
|
|
}
|
|
|
|
groups = append(groups, group)
|
|
|
|
}
|
|
|
|
return &GetGroupsBatchResponse{Groups: groups}, nil
|
|
|
|
}
|
|
|
|
func (s GroupsManagementServerImpl) Subscribe(ctx context.Context, req *SubscribeRequest) (*SubscribeResponse, error) {
|
|
|
|
err := s.handler.Subscribe(req.Groupid, req.Memberid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.AlreadyExists, "could not subscribe : %v", err)
|
|
|
|
}
|
|
|
|
return &SubscribeResponse{
|
|
|
|
Ok: true,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
func (s GroupsManagementServerImpl) Unsubscribe(context.Context, *UnsubscribeRequest) (*UnsubscribeResponse, error) {
|
|
|
|
return nil, status.Errorf(codes.Unimplemented, "method Unsubscribe not implemented")
|
|
|
|
}
|
|
|
|
func (s GroupsManagementServerImpl) mustEmbedUnimplementedGroupsManagementServer() {}
|
|
|
|
|
|
|
|
func Run(done chan error, cfg *viper.Viper, handler handlers.GroupsManagementHandler) {
|
|
|
|
var (
|
|
|
|
dev_env = cfg.GetBool("dev_env")
|
|
|
|
address = ":" + cfg.GetString("services.grpc.port")
|
|
|
|
)
|
|
|
|
fmt.Println("-> GRPC server on", address)
|
|
|
|
|
|
|
|
server := grpc.NewServer()
|
|
|
|
RegisterGroupsManagementServer(server, NewGroupsManagementServer(handler))
|
|
|
|
l, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dev_env {
|
|
|
|
reflection.Register(server)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := server.Serve(l); err != nil {
|
|
|
|
fmt.Println("gRPC service ended")
|
|
|
|
done <- err
|
|
|
|
}
|
|
|
|
}
|
2022-12-19 08:13:56 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
func (s GroupsManagementServerImpl) AddGroupMember(ctx context.Context, req *AddGroupMemberRequest) (*AddGroupMemberResponse, error) {
|
|
|
|
|
|
|
|
g := req.Group.ToStorageType()
|
|
|
|
group, err := s.handler.AddGroupMember(g)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.AlreadyExists, "group creation failed : %v", err)
|
|
|
|
}
|
|
|
|
response, err := GroupFromStorageTypeMember(group)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.Internal, "issue while retrieving group : %v", err)
|
|
|
|
}
|
|
|
|
return &AddGroupMemberResponse{Group: response}, nil
|
|
|
|
}
|
|
|
|
func (s GroupsManagementServerImpl) GetGroupMember(ctx context.Context, req *GetGroupMemberRequest) (*GetGroupMemberResponse, error) {
|
|
|
|
group, err := s.handler.GetGroupMember(req.Id)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving group : %v", err)
|
|
|
|
}
|
|
|
|
response, err := GroupFromStorageTypeMember(group)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.Internal, "issue while retrieving group : %v", err)
|
|
|
|
}
|
|
|
|
return &GetGroupMemberResponse{Group: response}, nil
|
|
|
|
}
|
|
|
|
func (s GroupsManagementServerImpl) GetGroupsMember(ctx context.Context, req *GetGroupsMemberRequest) (*GetGroupsMemberResponse, error) {
|
|
|
|
responses, err := s.handler.GetGroupsMember(req.Namespaces)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "could not get groups : %v", err)
|
|
|
|
}
|
|
|
|
var groups []*GroupMember
|
|
|
|
|
|
|
|
for _, g := range responses {
|
|
|
|
group, err := GroupFromStorageTypeMember(&g)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "could not get groups : %v", err)
|
|
|
|
}
|
|
|
|
groups = append(groups, group)
|
|
|
|
}
|
|
|
|
return &GetGroupsMemberResponse{Groups: groups}, nil
|
|
|
|
}
|
|
|
|
func (s GroupsManagementServerImpl) GetGroupsBatchMember(ctx context.Context, req *GetGroupsBatchMemberRequest) (*GetGroupsBatchMemberResponse, error) {
|
|
|
|
responses, err := s.handler.GetGroupsBatchMember(req.Groupids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "could not get groups : %v", err)
|
|
|
|
}
|
|
|
|
var groups []*GroupMember
|
|
|
|
|
|
|
|
for _, g := range responses {
|
|
|
|
group, err := GroupFromStorageTypeMember(&g)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "could not get groups : %v", err)
|
|
|
|
}
|
|
|
|
groups = append(groups, group)
|
|
|
|
}
|
|
|
|
return &GetGroupsBatchMemberResponse{Groups: groups}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s GroupsManagementServerImpl) SubscribeMember(ctx context.Context, req *SubscribeMemberRequest) (*SubscribeMemberResponse, error) {
|
|
|
|
g := req.Group.ToStorageType()
|
|
|
|
// err := s.handler.SubscribeMember(g)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, status.Errorf(codes.AlreadyExists, "could not subscribe : %v", err)
|
|
|
|
// }
|
|
|
|
// return &SubscribeMemberResponse{
|
|
|
|
// Ok: true,
|
|
|
|
// }, nil
|
|
|
|
|
|
|
|
group, err := s.handler.SubscribeMember(g)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.AlreadyExists, "group creation failed : %v", err)
|
|
|
|
}
|
|
|
|
response, err := GroupFromStorageTypeMember(group)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.Internal, "issue while retrieving group : %v", err)
|
|
|
|
}
|
|
|
|
return &SubscribeMemberResponse{Group: response}, nil
|
|
|
|
|
|
|
|
}
|