diff --git a/assembly/locator.go b/assembly/locator.go index b7cd43c..e5a9152 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -189,10 +189,10 @@ func (l Locator) registerEndpoint( } paths := []string{ - domain.ApiPrefix + routeKey.CleanPath, // стандартный маршрут c /api + routeKey.CleanPath, // стандартный маршрут } - if !routeKey.WithApiPrefix { - paths = append(paths, routeKey.CleanPath) // для межмодульного взаимодействия + if routeKey.WithApiPrefix { + paths = append(paths, domain.ApiPrefix+routeKey.CleanPath) // для endpoint'ов с /api } for _, path := range paths { diff --git a/domain/const.go b/domain/const.go index 0408965..f2feb41 100644 --- a/domain/const.go +++ b/domain/const.go @@ -1,6 +1,5 @@ package domain const ( - ApiPrefix = "/api" - GrpcApiPrefix = "api/" + ApiPrefix = "/api" ) diff --git a/service/proxy/grpc.go b/service/proxy/grpc.go index d9b485e..6a65513 100644 --- a/service/proxy/grpc.go +++ b/service/proxy/grpc.go @@ -57,12 +57,15 @@ func NewGrpc(cli *client.Client) Grpc { func (p Grpc) PrepareEndpoint(endpoint string, proxyApiPrefix bool) string { endpoint = strings.TrimPrefix(endpoint, "/") - if proxyApiPrefix { + if !proxyApiPrefix { return endpoint } - // api/endpoint -> endpoint - return strings.TrimPrefix(endpoint, domain.GrpcApiPrefix) + if strings.HasPrefix(endpoint, "api/") { + return endpoint + } + + return "api/" + endpoint } func (p Grpc) Handle(ctx *domain.RequestContext) error { diff --git a/service/proxy/http.go b/service/proxy/http.go index 9be81c5..a9a2dbc 100644 --- a/service/proxy/http.go +++ b/service/proxy/http.go @@ -46,10 +46,15 @@ func NewHttp(addresses []string) Http { } func (p Http) PrepareEndpoint(endpoint string, proxyApiPrefix bool) string { - if proxyApiPrefix { + if !proxyApiPrefix { return endpoint } - return strings.TrimPrefix(endpoint, domain.ApiPrefix) + + if strings.HasPrefix(endpoint, domain.ApiPrefix) { + return endpoint + } + + return domain.ApiPrefix + endpoint } func (p Http) Handle(ctx *domain.RequestContext) error { diff --git a/tests/acceptance_test.go b/tests/acceptance_test.go index c64994d..abc0ca7 100644 --- a/tests/acceptance_test.go +++ b/tests/acceptance_test.go @@ -47,7 +47,7 @@ func (s *AcceptanceTestSuite) SetupSuite() { var grpcMockAddr string s.grpcMock, grpcMockAddr = newGrpcMockWithEndpoints(t, map[string]any{ - "grpc/alive_backend/endpoint": func() string { return "GRPC_OK" }, + "api/grpc/alive_backend/endpoint": func() string { return "GRPC_OK" }, "api/grpc/alive_backend/endpoint_v2": func() string { return "GRPC_API" }, "grpc/alive_backend/endpoint_with_error": func() error { return status.Error(codes.FailedPrecondition, "test error") @@ -72,7 +72,7 @@ func (s *AcceptanceTestSuite) SetupSuite() { Version: "1.0.0", LibVersion: "1.0.0", Endpoints: []cluster.EndpointDescriptor{ - {Path: "grpc/alive_backend/endpoint"}, + {Path: "api/grpc/alive_backend/endpoint"}, {Path: "api/grpc/alive_backend/endpoint_v2"}, {Path: "grpc/alive_backend/endpoint_with_error"}, }, @@ -159,12 +159,10 @@ func (s *AcceptanceTestSuite) TestUnknownEndpoint() { } func (s *AcceptanceTestSuite) TestDeadBackend() { - s.assertStatus(http.MethodPost, "/api/dead_backend/endpoint", http.StatusServiceUnavailable) s.assertStatus(http.MethodPost, "/dead_backend/endpoint", http.StatusServiceUnavailable) } func (s *AcceptanceTestSuite) TestGrpcReuseEndpoint() { - s.assertJsonResponse(http.MethodPost, "api/reuse_backend/endpoint", "REUSE") s.assertJsonResponse(http.MethodPost, "reuse_backend/endpoint", "REUSE") } @@ -175,24 +173,23 @@ func (s *AcceptanceTestSuite) TestGrpcAliveEndpoint() { func (s *AcceptanceTestSuite) TestGrpcAliveApiEndpoint() { s.assertJsonResponse(http.MethodPost, "api/grpc/alive_backend/endpoint_v2", "GRPC_API") + s.assertJsonResponse(http.MethodPost, "grpc/alive_backend/endpoint_v2", "GRPC_API") } func (s *AcceptanceTestSuite) TestGrpcEndpointWithError() { - s.assertStatus(http.MethodPost, "api/grpc/alive_backend/endpoint_with_error", http.StatusPreconditionFailed) s.assertStatus(http.MethodPost, "grpc/alive_backend/endpoint_with_error", http.StatusPreconditionFailed) } func (s *AcceptanceTestSuite) TestHttpAliveEndpoint() { - s.assertJsonResponse(http.MethodGet, "/api/alive_backend/endpoint", "HTTP_OK") s.assertJsonResponse(http.MethodGet, "/alive_backend/endpoint", "HTTP_OK") } func (s *AcceptanceTestSuite) TestHttpAliveApiEndpoint() { s.assertJsonResponse(http.MethodGet, "/api/alive_backend/endpoint_v2", "HTTP_API") + s.assertJsonResponse(http.MethodGet, "/alive_backend/endpoint_v2", "HTTP_API") } func (s *AcceptanceTestSuite) TestHttpEndpointWithError() { - s.assertStatus(http.MethodPost, "/api/alive_backend/endpoint_with_error", http.StatusPreconditionFailed) s.assertStatus(http.MethodPost, "/alive_backend/endpoint_with_error", http.StatusPreconditionFailed) }