carpool-incentives/grpc/server/server.go

98 lines
2.8 KiB
Go
Raw Normal View History

2023-03-12 23:23:59 +00:00
package grpcserver
import (
"context"
"net"
"git.coopgo.io/coopgo-platform/carpool-incentives/grpc/proto"
"git.coopgo.io/coopgo-platform/carpool-incentives/handlers/incentives"
"github.com/rs/zerolog/log"
2023-03-12 23:23:59 +00:00
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
)
type CarpoolIncentivesServerImpl struct {
IncentivesHandler *incentives.IncentivesHandler
proto.UnimplementedCarpoolIncentivesServer
}
func NewCarpoolIncentivesServer(incentivesHandler *incentives.IncentivesHandler) *CarpoolIncentivesServerImpl {
return &CarpoolIncentivesServerImpl{
IncentivesHandler: incentivesHandler,
}
}
func (s CarpoolIncentivesServerImpl) GetAvailableIncentives(ctx context.Context, req *proto.GetAvailableIncentivesRequest) (*proto.GetAvailableIncentivesResponse, error) {
results := []*proto.Incentive{}
incentives, err := s.IncentivesHandler.GetAvailableIncentives(req.Userid)
if err != nil {
return nil, status.Errorf(codes.Internal, "error in GetAvailableIncentives : ", err)
}
for _, i := range incentives {
incentive := IncentiveFromStorage(i)
results = append(results, &incentive)
}
return &proto.GetAvailableIncentivesResponse{
Incentives: results,
}, nil
}
func (s CarpoolIncentivesServerImpl) GetInitiatedIncentives(ctx context.Context, req *proto.GetInitiatedIncentivesRequest) (*proto.GetInitiatedIncentivesResponse, error) {
results := []*proto.Incentive{}
incentives, err := s.IncentivesHandler.GetInitiatedIncentives(req.Userid)
if err != nil {
return nil, status.Errorf(codes.Internal, "error in GetInitiatedIncentives : ", err)
}
for _, i := range incentives {
incentive := IncentiveFromStorage(i)
results = append(results, &incentive)
}
return &proto.GetInitiatedIncentivesResponse{
Incentives: results,
}, nil
}
func (s CarpoolIncentivesServerImpl) SubscribeIncentive(ctx context.Context, req *proto.SubscribeIncentiveRequest) (*proto.SubscribeIncentiveResponse, error) {
data := req.Data.AsMap()
res, err := s.IncentivesHandler.SubscribeIncentive(req.Incentiveid, req.Userid, req.IdentityVerificationIds, data, req.Declined)
if err != nil {
return nil, err
}
return &proto.SubscribeIncentiveResponse{
Subscriptionid: res.ID,
}, nil
}
func Run(done chan error, cfg *viper.Viper, incentivesHandler *incentives.IncentivesHandler) {
var (
dev_env = cfg.GetBool("dev_env")
address = ":" + cfg.GetString("services.grpc.port")
)
server := grpc.NewServer()
proto.RegisterCarpoolIncentivesServer(server, NewCarpoolIncentivesServer(incentivesHandler))
l, err := net.Listen("tcp", address)
if err != nil {
log.Fatal().Err(err).Msg("could not create grpc server")
return
2023-03-12 23:23:59 +00:00
}
if dev_env {
reflection.Register(server)
}
if err := server.Serve(l); err != nil {
log.Error().Err(err).Msg("grpc service ended")
2023-03-12 23:23:59 +00:00
done <- err
}
}