silvermobi/servers/grpcapi/server/geo.go

107 lines
2.8 KiB
Go
Raw Permalink Normal View History

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