132 lines
4.7 KiB
Go
132 lines
4.7 KiB
Go
package grpcserver
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
|
|
"git.coopgo.io/coopgo-platform/carpool-service/handler"
|
|
"git.coopgo.io/coopgo-platform/carpool-service/servers/grpc/proto"
|
|
"github.com/paulmach/orb/geojson"
|
|
"github.com/rs/zerolog/log"
|
|
"github.com/spf13/viper"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/reflection"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type CarpoolServiceServerImpl struct {
|
|
Handler *handler.CarpoolServiceHandler
|
|
|
|
proto.UnimplementedCarpoolServiceServer
|
|
}
|
|
|
|
func NewCarpoolServiceServer(handler *handler.CarpoolServiceHandler) *CarpoolServiceServerImpl {
|
|
return &CarpoolServiceServerImpl{
|
|
Handler: handler,
|
|
}
|
|
}
|
|
|
|
func (s *CarpoolServiceServerImpl) CreateRegularRoutes(ctx context.Context, req *proto.CreateRegularRoutesRequest) (*proto.CreateRegularRoutesResponse, error) {
|
|
log.Debug().Msg("grpc CarpoolService - CreateTrips")
|
|
routes := []*geojson.FeatureCollection{}
|
|
|
|
for _, r := range req.Routes {
|
|
route, err := geojson.UnmarshalFeatureCollection([]byte(r.Serialized))
|
|
if err == nil {
|
|
routes = append(routes, route)
|
|
}
|
|
}
|
|
|
|
if len(routes) == 0 {
|
|
return nil, status.Errorf(codes.InvalidArgument, "no route to create")
|
|
}
|
|
|
|
if err := s.Handler.CreateRegularRoutes(routes); err != nil {
|
|
return nil, status.Errorf(codes.Internal, "could not create routes in CreateRegularRoutes : %s", err.Error())
|
|
}
|
|
|
|
return &proto.CreateRegularRoutesResponse{}, nil
|
|
}
|
|
|
|
func (s *CarpoolServiceServerImpl) DeleteRegularRoutes(context.Context, *proto.DeleteRegularRoutesRequest) (*proto.DeleteRegularRoutesResponse, error) {
|
|
return nil, status.Errorf(codes.Unimplemented, "method DeleteTrips not implemented")
|
|
}
|
|
|
|
func (s *CarpoolServiceServerImpl) GetUserPlanning(ctx context.Context, req *proto.GetUserPlanningRequest) (*proto.GetUserPlanningResponse, error) {
|
|
log.Debug().Msg("grpc CarpoolService - GetRegularUserRoutes")
|
|
|
|
planned_schedules, err := s.Handler.GetUserPlanning(req.UserId, req.MinDepartureDate.AsTime(), req.MaxDepartureDate.AsTime())
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "could not get user planning : %s", err.Error())
|
|
}
|
|
|
|
results := map[string]*proto.CarpoolRoutesCollection{}
|
|
|
|
for k, scheds := range planned_schedules {
|
|
|
|
results[k] = &proto.CarpoolRoutesCollection{
|
|
Collection: []*proto.CarpoolFeatureCollection{},
|
|
}
|
|
|
|
for _, s := range scheds {
|
|
s.Route.ExtraMembers["departure_date"] = s.DepartureDate
|
|
fcraw, _ := s.Route.FeatureCollection().MarshalJSON()
|
|
results[k].Collection = append(results[k].Collection, &proto.CarpoolFeatureCollection{
|
|
Serialized: string(fcraw),
|
|
})
|
|
}
|
|
}
|
|
|
|
return &proto.GetUserPlanningResponse{
|
|
RoutesByDates: results,
|
|
}, nil
|
|
}
|
|
|
|
func (s *CarpoolServiceServerImpl) DriverJourneys(context.Context, *proto.DriverJourneysRequest) (*proto.DriverJourneysResponse, error) {
|
|
return nil, status.Errorf(codes.Unimplemented, "method DriverJourneys not implemented")
|
|
}
|
|
func (s *CarpoolServiceServerImpl) PassengerJourneys(context.Context, *proto.PassengerJourneysRequest) (*proto.PassengerJourneysResponse, error) {
|
|
return nil, status.Errorf(codes.Unimplemented, "method PassengerJourneys not implemented")
|
|
}
|
|
func (s *CarpoolServiceServerImpl) DriverRegularTrips(context.Context, *proto.DriverRegularTripsRequest) (*proto.DriverRegularTripsResponse, error) {
|
|
return nil, status.Errorf(codes.Unimplemented, "method DriverRegularTrips not implemented")
|
|
}
|
|
func (s *CarpoolServiceServerImpl) PassengerRegularTrips(context.Context, *proto.PassengerRegularTripsRequest) (*proto.PassengerRegularTripsResponse, error) {
|
|
return nil, status.Errorf(codes.Unimplemented, "method PassengerRegularTrips not implemented")
|
|
}
|
|
func (s *CarpoolServiceServerImpl) CreateBooking(context.Context, *proto.CreateBookingRequest) (*proto.CreateBookingResponse, error) {
|
|
return nil, status.Errorf(codes.Unimplemented, "method CreateBooking not implemented")
|
|
}
|
|
func (s *CarpoolServiceServerImpl) UpdateBooking(context.Context, *proto.UpdateBookingRequest) (*proto.UpdateBookingResponse, error) {
|
|
return nil, status.Errorf(codes.Unimplemented, "method UpdateBooking not implemented")
|
|
}
|
|
func (s *CarpoolServiceServerImpl) GetBooking(context.Context, *proto.GetBookingRequest) (*proto.GetBookingResponse, error) {
|
|
return nil, status.Errorf(codes.Unimplemented, "method GetBooking not implemented")
|
|
}
|
|
|
|
func Run(done chan error, cfg *viper.Viper, handler *handler.CarpoolServiceHandler) {
|
|
var (
|
|
dev_env = cfg.GetBool("dev_env")
|
|
address = ":" + cfg.GetString("services.grpc.port")
|
|
)
|
|
|
|
server := grpc.NewServer()
|
|
proto.RegisterCarpoolServiceServer(server, NewCarpoolServiceServer(handler))
|
|
l, err := net.Listen("tcp", address)
|
|
if err != nil {
|
|
log.Fatal().Err(err)
|
|
return
|
|
}
|
|
|
|
if dev_env {
|
|
reflection.Register(server)
|
|
}
|
|
|
|
if err := server.Serve(l); err != nil {
|
|
fmt.Println("gRPC service ended")
|
|
done <- err
|
|
}
|
|
}
|