silvermobi/servers/grpcapi/server/geo.go

100 lines
2.9 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"
"io"
)
func (s SilvermobiGRPCService) GeoAutocomplete(ctx context.Context, in *grpcproto.GeoAutocompleteRequest) (resp *grpcproto.GeoAutocompleteResponse, err error) {
log.Info().
Str("text", in.Text).
Msg("GeoAutocompleteRequest")
requestid := uuid.NewString()
log.Debug().Str("requestid", requestid).Msg("GRPC GeoAutocomplete start")
if err == io.EOF {
log.Debug().Str("requestid", requestid).Msg("GRPC GeoAutocomplete EOF")
return nil, err
}
if err != nil {
log.Error().Str("requestid", requestid).Err(err).Msg("GRPC GeoAutocomplete other error")
return nil, err
}
results, err := s.Handler.GeoAutocomplete(in.Text, in.Lat, in.Lon)
if err != nil {
log.Error().Str("requestid", requestid).Err(err).Msg("GRPC GeoAutocomplete geocoding error")
return nil, err
}
rawfc, err := results.MarshalJSON()
if 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) {
locations_raw, ok := req.Locations.(*grpcproto.GeoRouteRequest_LocationsRaw)
if !ok {
return nil, status.Errorf(codes.InvalidArgument, "could not read departure")
}
locations, err := geojson.UnmarshalFeatureCollection([]byte(locations_raw.LocationsRaw))
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
route, err := s.Handler.GeoRoute(*locations)
if err != nil {
return nil, err
}
return &grpcproto.GeoRouteResponse{
Polyline: route.Polyline.String(),
}, nil
}
func (s SilvermobiGRPCService) GeoRouteWithReturn(ctx context.Context, req *grpcproto.GeoRouteWithReturnRequest) (*grpcproto.GeoRouteWithReturnResponse, error) {
locations_raw, ok := req.Locations.(*grpcproto.GeoRouteWithReturnRequest_LocationsRaw)
if !ok {
return nil, status.Errorf(codes.InvalidArgument, "could not read departure")
}
locations, err := geojson.UnmarshalFeatureCollection([]byte(locations_raw.LocationsRaw))
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
route, err := s.Handler.GeoRoute(*locations)
if err != nil {
return nil, err
}
return_route, err := s.Handler.GeoReturnRoute(*locations)
if err != nil {
return nil, err
}
return &grpcproto.GeoRouteWithReturnResponse{
Polyline: route.Polyline.String(),
ReturnPolyline: return_route.Polyline.String(),
}, nil
}