silvermobi/servers/grpcapi/server/geo.go

106 lines
2.8 KiB
Go

package grpcserver
import (
"context"
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
"github.com/google/uuid"
"github.com/paulmach/orb/geojson"
"github.com/rs/zerolog/log"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s SilvermobiGRPCService) GeoAutocomplete(ctx context.Context,
in *grpcproto.GeoAutocompleteRequest) (resp *grpcproto.GeoAutocompleteResponse, err error) {
var (
results *geojson.FeatureCollection
rawfc []byte
)
log.Info().
Str("text", in.Text).
Msg("GeoAutocompleteRequest")
requestID := uuid.NewString()
log.Debug().Str("requestID", requestID).Msg("GRPC GeoAutocomplete start")
if results, err = s.Handler.GeoAutocomplete(in.Text); err != nil {
log.Error().Str("requestID", requestID).Err(err).Msg("GRPC GeoAutocomplete geocoding error")
return nil, err
}
if rawfc, err = results.MarshalJSON(); err != nil {
log.Error().Str("requestID", requestID).Err(err).Msg("GRPC GeoAutocomplete " +
"protocol buffer conversion error")
return nil, err
}
resp = &grpcproto.GeoAutocompleteResponse{
FeatureCollection: &grpcproto.GeoAutocompleteResponse_FeatureCollectionRaw{
FeatureCollectionRaw: string(rawfc),
},
}
return resp, nil
}
func (s SilvermobiGRPCService) GeoRoute(ctx context.Context,
req *grpcproto.GeoRouteRequest) (*grpcproto.GeoRouteResponse, error) {
locationsRaw, ok := req.Locations.(*grpcproto.GeoRouteRequest_LocationsRaw)
if !ok {
return nil, status.Errorf(codes.InvalidArgument, "could not read departure")
}
locations, err := geojson.UnmarshalFeatureCollection([]byte(locationsRaw.LocationsRaw))
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if _, err = s.Handler.GeoRoute(*locations); err != nil {
return nil, err
}
route, _ := s.Handler.GeoRoute(*locations)
return &grpcproto.GeoRouteResponse{
Polyline: route.Polyline.String(),
}, nil
}
func (s SilvermobiGRPCService) GeoRouteWithReturn(ctx context.Context,
req *grpcproto.GeoRouteWithReturnRequest) (*grpcproto.GeoRouteWithReturnResponse, error) {
locationsRaw, ok := req.Locations.(*grpcproto.GeoRouteWithReturnRequest_LocationsRaw)
if !ok {
return nil, status.Errorf(codes.InvalidArgument, "could not read departure")
}
locations, err := geojson.UnmarshalFeatureCollection([]byte(locationsRaw.LocationsRaw))
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if _, err = s.Handler.GeoRoute(*locations); err != nil {
return nil, err
}
route, _ := s.Handler.GeoRoute(*locations)
if _, err = s.Handler.GeoReturnRoute(*locations); err != nil {
return nil, err
}
returnRoute, _ := s.Handler.GeoReturnRoute(*locations)
return &grpcproto.GeoRouteWithReturnResponse{
Polyline: route.Polyline.String(),
ReturnPolyline: returnRoute.Polyline.String(),
}, nil
}