package grpcapi import ( context "context" "fmt" "log" "net" "git.coopgo.io/coopgo-platform/fleets/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 FleetsServerImpl struct { handler handlers.FleetsHandler } func NewFleetsServer(h handlers.FleetsHandler) *FleetsServerImpl { return &FleetsServerImpl{ handler: h, } } func (s FleetsServerImpl) AddVehicle(ctx context.Context, req *AddVehicleRequest) (*AddVehicleResponse, error) { v := req.Vehicle.ToStorageType() vehicle, err := s.handler.AddVehicle(v) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.AlreadyExists, "vehicle creation failed : %v", err) } response, err := VehicleFromStorageType(vehicle) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.Internal, "issue while retrieving vehicle : %v", err) } return &AddVehicleResponse{Vehicle: response}, nil } func (s FleetsServerImpl) GetVehicle(ctx context.Context, req *GetVehicleRequest) (*GetVehicleResponse, error) { vehicle, err := s.handler.GetVehicle(req.Vehicleid) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving vehicle : %v", err) } response, err := VehicleFromStorageType(vehicle) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.Internal, "issue while retrieving vehicle : %v", err) } return &GetVehicleResponse{Vehicle: response}, nil } func (s FleetsServerImpl) GetVehicles(ctx context.Context, req *GetVehiclesRequest) (*GetVehiclesResponse, error) { responses, err := s.handler.GetVehicles(req.Namespaces) if err != nil { return nil, status.Errorf(codes.NotFound, "could not get vehicles : %v", err) } var vehicles []*Vehicle for _, v := range responses { vehicle, err := VehicleFromStorageType(&v) if err != nil { return nil, status.Errorf(codes.Internal, "could not get vehicle : %v", err) } vehicles = append(vehicles, vehicle) } return &GetVehiclesResponse{Vehicles: vehicles}, nil } func (s FleetsServerImpl) UpdateVehicle(context.Context, *UpdateVehicleRequest) (*UpdateVehicleResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method UpdateVehicle not implemented") } func (s FleetsServerImpl) CreateBooking(ctx context.Context, req *CreateBookingRequest) (*CreateBookingResponse, error) { b := req.Booking.ToStorageType() booking, err := s.handler.AddBooking(b) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.AlreadyExists, "booking creation failed : %v", err) } response, err := BookingFromStorageType(booking) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.Internal, "issue while retrieving booking : %v", err) } return &CreateBookingResponse{Booking: response}, nil } func (s FleetsServerImpl) GetBooking(ctx context.Context, req *GetBookingRequest) (*GetBookingResponse, error) { booking, err := s.handler.GetBooking(req.Bookingid) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving booking : %v", err) } response, err := BookingFromStorageType(booking) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.Internal, "issue while retrieving booking : %v", err) } return &GetBookingResponse{Booking: response}, nil } func (s FleetsServerImpl) UpdateBooking(context.Context, *UpdateBookingRequest) (*UpdateBookingResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method UpdateBooking not implemented") } func (s FleetsServerImpl) DeleteBooking(ctx context.Context, req *DeleteBookingRequest) (*DeleteBookingResponse, error) { if err := s.handler.DeleteBooking(req.Id); err != nil { fmt.Println(err) return nil, status.Errorf(codes.Internal, "issue while retrieving booking : %v", err) } return &DeleteBookingResponse{ Ok: true, }, nil } func (s FleetsServerImpl) FindVehicle(context.Context, *FindVehicleRequest) (*FindVehicleResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method FindVehicle not implemented") } func (s FleetsServerImpl) mustEmbedUnimplementedFleetsServer() {} func Run(done chan error, cfg *viper.Viper, handler handlers.FleetsHandler) { var ( dev_env = cfg.GetBool("dev_env") address = ":" + cfg.GetString("services.grpc.port") ) fmt.Println("-> GRPC server on", address) server := grpc.NewServer() RegisterFleetsServer(server, NewFleetsServer(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 } }