125 lines
3.8 KiB
Go
125 lines
3.8 KiB
Go
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) {
|
|
responses, err := s.handler.GetGroups(req.Namespaces)
|
|
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
|
|
}
|
|
}
|