MSP-7621 remove endpoint duplication with /api
This commit is contained in:
1 parent
2c7aa1ce6d
commit
591954bbae
5 files changed
+20
-16
No files matched your search
+3
-3
@@ -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 {
|
||||
|
||||
@@ -2,5 +2,4 @@ package domain
|
||||
|
||||
const (
|
||||
ApiPrefix = "/api"
|
||||
GrpcApiPrefix = "api/"
|
||||
)
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user