initial commit
This commit is contained in:
13
grpc/server/incentives.go
Normal file
13
grpc/server/incentives.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"git.coopgo.io/coopgo-platform/carpool-incentives/grpc/proto"
|
||||
"git.coopgo.io/coopgo-platform/carpool-incentives/storage"
|
||||
)
|
||||
|
||||
func IncentiveFromStorage(incentive storage.Incentive) proto.Incentive {
|
||||
return proto.Incentive{
|
||||
Id: incentive.ID,
|
||||
Name: incentive.Name,
|
||||
}
|
||||
}
|
||||
98
grpc/server/server.go
Normal file
98
grpc/server/server.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"git.coopgo.io/coopgo-platform/carpool-incentives/grpc/proto"
|
||||
"git.coopgo.io/coopgo-platform/carpool-incentives/handlers/incentives"
|
||||
"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")
|
||||
)
|
||||
//fmt.Println("-> GRPC server on", address)
|
||||
|
||||
server := grpc.NewServer()
|
||||
proto.RegisterCarpoolIncentivesServer(server, NewCarpoolIncentivesServer(incentivesHandler))
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user