2022-09-05 05:27:52 +00:00
|
|
|
package grpcapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
context "context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
2022-12-05 16:21:12 +00:00
|
|
|
"time"
|
2022-09-05 05:27:52 +00:00
|
|
|
|
|
|
|
"git.coopgo.io/coopgo-platform/agenda/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 AgendaServerImpl struct {
|
|
|
|
handler handlers.AgendaHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAgendaServer(h handlers.AgendaHandler) *AgendaServerImpl {
|
|
|
|
return &AgendaServerImpl{
|
|
|
|
handler: h,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s AgendaServerImpl) CreateEvent(ctx context.Context, req *CreateEventRequest) (*CreateEventResponse, error) {
|
|
|
|
e := req.Event.ToStorageType()
|
|
|
|
event, err := s.handler.CreateEvent(e)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.AlreadyExists, "event creation failed : %v", err)
|
|
|
|
}
|
|
|
|
response, err := EventFromStorageType(event)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.Internal, "issue while retrieving event : %v", err)
|
|
|
|
}
|
|
|
|
return &CreateEventResponse{Event: response}, nil
|
|
|
|
}
|
|
|
|
func (s AgendaServerImpl) GetEvent(ctx context.Context, req *GetEventRequest) (*GetEventResponse, error) {
|
|
|
|
event, err := s.handler.GetEvent(req.Id)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving event : %v", err)
|
|
|
|
}
|
|
|
|
response, err := EventFromStorageType(event)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.Internal, "issue while retrieving event : %v", err)
|
|
|
|
}
|
|
|
|
return &GetEventResponse{Event: response}, nil
|
|
|
|
}
|
|
|
|
func (s AgendaServerImpl) GetEvents(ctx context.Context, req *GetEventsRequest) (*GetEventsResponse, error) {
|
2022-12-05 16:21:12 +00:00
|
|
|
var mindate, maxdate *time.Time
|
|
|
|
|
|
|
|
if req.Mindate != nil {
|
|
|
|
m := req.Mindate.AsTime()
|
|
|
|
mindate = &m
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Maxdate != nil {
|
|
|
|
m := req.Maxdate.AsTime()
|
|
|
|
mindate = &m
|
|
|
|
}
|
|
|
|
|
|
|
|
responses, err := s.handler.GetEvents(req.Namespaces, mindate, maxdate)
|
2022-09-05 05:27:52 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.NotFound, "could not get events : %v", err)
|
|
|
|
}
|
|
|
|
var events []*Event
|
|
|
|
|
|
|
|
for _, e := range responses {
|
|
|
|
event, err := EventFromStorageType(&e)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.Internal, "could not get events : %v", err)
|
|
|
|
}
|
|
|
|
events = append(events, event)
|
|
|
|
}
|
|
|
|
return &GetEventsResponse{Events: events}, nil
|
|
|
|
}
|
|
|
|
func (s AgendaServerImpl) DeleteEvent(context.Context, *DeleteEventRequest) (*DeleteEventResponse, error) {
|
|
|
|
return nil, status.Errorf(codes.Unimplemented, "method DeleteEvent not implemented")
|
|
|
|
}
|
|
|
|
func (s AgendaServerImpl) SubscribeEvent(ctx context.Context, req *SubscribeEventRequest) (*SubscribeEventResponse, error) {
|
2022-12-05 16:21:12 +00:00
|
|
|
err := s.handler.SubscribeEvent(req.Eventid, req.Subscriber, req.Data.AsMap())
|
2022-09-05 05:27:52 +00:00
|
|
|
return &SubscribeEventResponse{
|
|
|
|
Ok: err != nil,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
func (s AgendaServerImpl) UnsubscribeEvent(context.Context, *UnsubscribeEventRequest) (*UnsubscribeEventResponse, error) {
|
|
|
|
return nil, status.Errorf(codes.Unimplemented, "method Unsubscribe not implemented")
|
|
|
|
}
|
|
|
|
func (s AgendaServerImpl) mustEmbedUnimplementedAgendaServer() {}
|
|
|
|
|
|
|
|
func Run(done chan error, cfg *viper.Viper, handler handlers.AgendaHandler) {
|
|
|
|
var (
|
|
|
|
dev_env = cfg.GetBool("dev_env")
|
|
|
|
address = ":" + cfg.GetString("services.grpc.port")
|
|
|
|
)
|
|
|
|
fmt.Println("-> GRPC server on", address)
|
|
|
|
|
|
|
|
server := grpc.NewServer()
|
|
|
|
RegisterAgendaServer(server, NewAgendaServer(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
|
|
|
|
}
|
|
|
|
}
|