From 80ba8575b504adc9377550f675bd8b49b4f2dce1 Mon Sep 17 00:00:00 2001 From: durachok Date: Thu, 9 May 2024 23:44:21 +0700 Subject: [PATCH 1/2] add radius --- cmd/server/main.go | 3 +-- internal/elastic_client.go | 6 ++++- internal/elastic_client_test.go | 4 +-- internal/handlers/coords_handler.go | 3 ++- pkg/api/elastic_service.pb.go | 40 +++++++++++++++++++---------- proto/elastic_service.proto | 1 + 6 files changed, 37 insertions(+), 20 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 3906177..bc7d28c 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -3,7 +3,7 @@ package main import ( "elastic-service/internal" "elastic-service/internal/handlers" - pb "elastic-service/pkg/api" // Import generated proto package + pb "elastic-service/pkg/api" "github.com/joho/godotenv" "google.golang.org/grpc" "google.golang.org/grpc/reflection" @@ -20,7 +20,6 @@ func main() { return } - // Get port from command-line argument or use default defaultPort := strconv.Itoa(internal.DefaultPort) configuredPort := os.Getenv("PORT") diff --git a/internal/elastic_client.go b/internal/elastic_client.go index 70af040..723d142 100644 --- a/internal/elastic_client.go +++ b/internal/elastic_client.go @@ -73,13 +73,17 @@ func (ec *ElasticsearchClient) SearchByName(name string) ([]*pb.City, error) { } // SearchByCoords searches for a city by coordinates -func (ec *ElasticsearchClient) SearchByCoords(coords *pb.Coords) ([]*pb.City, error) { +func (ec *ElasticsearchClient) SearchByCoords(coords *pb.Coords, radius string) ([]*pb.City, error) { distance := os.Getenv(geoDistance) if distance == "" { distance = defaultDistance } + if radius != "" { + distance = radius + } + query := fmt.Sprintf(`{ "query": { "bool": { diff --git a/internal/elastic_client_test.go b/internal/elastic_client_test.go index 0b4bd35..e205713 100644 --- a/internal/elastic_client_test.go +++ b/internal/elastic_client_test.go @@ -78,7 +78,7 @@ func TestSearchMoscowByCoords(t *testing.T) { Lon: 37.617777777778, } - cities, err := ecClient.SearchByCoords(moscowCoords) + cities, err := ecClient.SearchByCoords(moscowCoords, defaultDistance) assert.NoError(t, err, "Search by coords failed") assert.NotEmpty(t, cities, "Expected at least one city, got none") @@ -105,7 +105,7 @@ func TestSearchByNonExistentCoords(t *testing.T) { Lon: 1000.0, } - cities, err := ecClient.SearchByCoords(nonExistentCoords) + cities, err := ecClient.SearchByCoords(nonExistentCoords, defaultDistance) assert.NoError(t, err, "Search by non-existent coordinates failed") assert.Empty(t, cities, "Expected empty result for non-existent coordinates") } diff --git a/internal/handlers/coords_handler.go b/internal/handlers/coords_handler.go index 0582ad2..c78452a 100644 --- a/internal/handlers/coords_handler.go +++ b/internal/handlers/coords_handler.go @@ -10,6 +10,7 @@ func (s *Server) SearchByCoords(ctx context.Context, req *pb.CoordsRequest) (*pb lat := req.GetLat() lon := req.GetLon() + radius := req.GetRadius() coords, err := pb.NewCoords(lat, lon) if err != nil { @@ -17,7 +18,7 @@ func (s *Server) SearchByCoords(ctx context.Context, req *pb.CoordsRequest) (*pb return response, nil } - cities, err := s.client.SearchByCoords(coords) + cities, err := s.client.SearchByCoords(coords, radius) if err != nil { response.Code = pb.CitySearchCoordsResponse_INTERNAL_ERROR_COORDS return response, nil diff --git a/pkg/api/elastic_service.pb.go b/pkg/api/elastic_service.pb.go index d4ed768..1b92da1 100644 --- a/pkg/api/elastic_service.pb.go +++ b/pkg/api/elastic_service.pb.go @@ -420,8 +420,9 @@ type CoordsRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Lat float32 `protobuf:"fixed32,1,opt,name=lat,proto3" json:"lat,omitempty"` - Lon float32 `protobuf:"fixed32,2,opt,name=lon,proto3" json:"lon,omitempty"` + Lat float32 `protobuf:"fixed32,1,opt,name=lat,proto3" json:"lat,omitempty"` + Lon float32 `protobuf:"fixed32,2,opt,name=lon,proto3" json:"lon,omitempty"` + Radius *string `protobuf:"bytes,3,opt,name=radius,proto3,oneof" json:"radius,omitempty"` } func (x *CoordsRequest) Reset() { @@ -470,6 +471,13 @@ func (x *CoordsRequest) GetLon() float32 { return 0 } +func (x *CoordsRequest) GetRadius() string { + if x != nil && x.Radius != nil { + return *x.Radius + } + return "" +} + var File_proto_elastic_service_proto protoreflect.FileDescriptor var file_proto_elastic_service_proto_rawDesc = []byte{ @@ -518,20 +526,23 @@ var file_proto_elastic_service_proto_rawDesc = []byte{ 0x43, 0x4f, 0x4f, 0x52, 0x44, 0x53, 0x10, 0x03, 0x22, 0x25, 0x0a, 0x0f, 0x43, 0x69, 0x74, 0x79, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, - 0x33, 0x0a, 0x0d, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x5b, 0x0a, 0x0d, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6c, 0x61, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x02, 0x52, 0x03, 0x6c, 0x61, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6c, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x02, 0x52, - 0x03, 0x6c, 0x6f, 0x6e, 0x32, 0x94, 0x01, 0x0a, 0x0a, 0x43, 0x69, 0x74, 0x79, 0x53, 0x65, 0x61, - 0x72, 0x63, 0x68, 0x12, 0x41, 0x0a, 0x0c, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x42, 0x79, 0x4e, - 0x61, 0x6d, 0x65, 0x12, 0x14, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x43, 0x69, 0x74, 0x79, 0x4e, 0x61, - 0x6d, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1b, 0x2e, 0x61, 0x70, 0x69, 0x2e, - 0x43, 0x69, 0x74, 0x79, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x65, - 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x43, 0x0a, 0x0e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, - 0x42, 0x79, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x73, 0x12, 0x12, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x43, - 0x6f, 0x6f, 0x72, 0x64, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e, 0x61, - 0x70, 0x69, 0x2e, 0x43, 0x69, 0x74, 0x79, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x43, 0x6f, 0x6f, - 0x72, 0x64, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x07, 0x5a, 0x05, 0x2e, - 0x2f, 0x61, 0x70, 0x69, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x03, 0x6c, 0x6f, 0x6e, 0x12, 0x1b, 0x0a, 0x06, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x18, 0x03, + 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x06, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x88, 0x01, + 0x01, 0x42, 0x09, 0x0a, 0x07, 0x5f, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x32, 0x94, 0x01, 0x0a, + 0x0a, 0x43, 0x69, 0x74, 0x79, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x12, 0x41, 0x0a, 0x0c, 0x53, + 0x65, 0x61, 0x72, 0x63, 0x68, 0x42, 0x79, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x2e, 0x61, 0x70, + 0x69, 0x2e, 0x43, 0x69, 0x74, 0x79, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x1a, 0x1b, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x43, 0x69, 0x74, 0x79, 0x53, 0x65, 0x61, 0x72, + 0x63, 0x68, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x43, + 0x0a, 0x0e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x42, 0x79, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x73, + 0x12, 0x12, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x73, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x43, 0x69, 0x74, 0x79, 0x53, + 0x65, 0x61, 0x72, 0x63, 0x68, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x42, 0x07, 0x5a, 0x05, 0x2e, 0x2f, 0x61, 0x70, 0x69, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -654,6 +665,7 @@ func file_proto_elastic_service_proto_init() { } } } + file_proto_elastic_service_proto_msgTypes[5].OneofWrappers = []interface{}{} type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ diff --git a/proto/elastic_service.proto b/proto/elastic_service.proto index 4bf2a8a..a45efbc 100644 --- a/proto/elastic_service.proto +++ b/proto/elastic_service.proto @@ -48,6 +48,7 @@ message CityNameRequest { message CoordsRequest { float lat = 1; float lon = 2; + optional string radius = 3; } From 8faf6c0d527006d9218166a2bd73654b56e67526 Mon Sep 17 00:00:00 2001 From: durachok Date: Thu, 9 May 2024 23:49:49 +0700 Subject: [PATCH 2/2] fix format --- proto/elastic_service.proto | 2 -- 1 file changed, 2 deletions(-) diff --git a/proto/elastic_service.proto b/proto/elastic_service.proto index a45efbc..d4991a4 100644 --- a/proto/elastic_service.proto +++ b/proto/elastic_service.proto @@ -39,8 +39,6 @@ message CitySearchCoordsResponse{ repeated City options = 2; } - - message CityNameRequest { string name = 1; }