MSP-7621 remove endpoint duplication with /api
This commit is contained in:
1 parent
2c7aa1ce6d
commit
591954bbae
5 files changed
+21
-17
No files matched your search
+3
-3
@@ -189,10 +189,10 @@ func (l Locator) registerEndpoint(
|
|||||||
}
|
}
|
||||||
|
|
||||||
paths := []string{
|
paths := []string{
|
||||||
domain.ApiPrefix + routeKey.CleanPath, // стандартный маршрут c /api
|
routeKey.CleanPath, // стандартный маршрут
|
||||||
}
|
}
|
||||||
if !routeKey.WithApiPrefix {
|
if routeKey.WithApiPrefix {
|
||||||
paths = append(paths, routeKey.CleanPath) // для межмодульного взаимодействия
|
paths = append(paths, domain.ApiPrefix+routeKey.CleanPath) // для endpoint'ов с /api
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
|
|||||||
+1
-2
@@ -1,6 +1,5 @@
|
|||||||
package domain
|
package domain
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ApiPrefix = "/api"
|
ApiPrefix = "/api"
|
||||||
GrpcApiPrefix = "api/"
|
|
||||||
)
|
)
|
||||||
@@ -57,12 +57,15 @@ func NewGrpc(cli *client.Client) Grpc {
|
|||||||
|
|
||||||
func (p Grpc) PrepareEndpoint(endpoint string, proxyApiPrefix bool) string {
|
func (p Grpc) PrepareEndpoint(endpoint string, proxyApiPrefix bool) string {
|
||||||
endpoint = strings.TrimPrefix(endpoint, "/")
|
endpoint = strings.TrimPrefix(endpoint, "/")
|
||||||
if proxyApiPrefix {
|
if !proxyApiPrefix {
|
||||||
return endpoint
|
return endpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
// api/endpoint -> endpoint
|
if strings.HasPrefix(endpoint, "api/") {
|
||||||
return strings.TrimPrefix(endpoint, domain.GrpcApiPrefix)
|
return endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
return "api/" + endpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Grpc) Handle(ctx *domain.RequestContext) error {
|
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 {
|
func (p Http) PrepareEndpoint(endpoint string, proxyApiPrefix bool) string {
|
||||||
if proxyApiPrefix {
|
if !proxyApiPrefix {
|
||||||
return endpoint
|
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 {
|
func (p Http) Handle(ctx *domain.RequestContext) error {
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ func (s *AcceptanceTestSuite) SetupSuite() {
|
|||||||
|
|
||||||
var grpcMockAddr string
|
var grpcMockAddr string
|
||||||
s.grpcMock, grpcMockAddr = newGrpcMockWithEndpoints(t, map[string]any{
|
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" },
|
"api/grpc/alive_backend/endpoint_v2": func() string { return "GRPC_API" },
|
||||||
"grpc/alive_backend/endpoint_with_error": func() error {
|
"grpc/alive_backend/endpoint_with_error": func() error {
|
||||||
return status.Error(codes.FailedPrecondition, "test error")
|
return status.Error(codes.FailedPrecondition, "test error")
|
||||||
@@ -72,7 +72,7 @@ func (s *AcceptanceTestSuite) SetupSuite() {
|
|||||||
Version: "1.0.0",
|
Version: "1.0.0",
|
||||||
LibVersion: "1.0.0",
|
LibVersion: "1.0.0",
|
||||||
Endpoints: []cluster.EndpointDescriptor{
|
Endpoints: []cluster.EndpointDescriptor{
|
||||||
{Path: "grpc/alive_backend/endpoint"},
|
{Path: "api/grpc/alive_backend/endpoint"},
|
||||||
{Path: "api/grpc/alive_backend/endpoint_v2"},
|
{Path: "api/grpc/alive_backend/endpoint_v2"},
|
||||||
{Path: "grpc/alive_backend/endpoint_with_error"},
|
{Path: "grpc/alive_backend/endpoint_with_error"},
|
||||||
},
|
},
|
||||||
@@ -159,12 +159,10 @@ func (s *AcceptanceTestSuite) TestUnknownEndpoint() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *AcceptanceTestSuite) TestDeadBackend() {
|
func (s *AcceptanceTestSuite) TestDeadBackend() {
|
||||||
s.assertStatus(http.MethodPost, "/api/dead_backend/endpoint", http.StatusServiceUnavailable)
|
|
||||||
s.assertStatus(http.MethodPost, "/dead_backend/endpoint", http.StatusServiceUnavailable)
|
s.assertStatus(http.MethodPost, "/dead_backend/endpoint", http.StatusServiceUnavailable)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *AcceptanceTestSuite) TestGrpcReuseEndpoint() {
|
func (s *AcceptanceTestSuite) TestGrpcReuseEndpoint() {
|
||||||
s.assertJsonResponse(http.MethodPost, "api/reuse_backend/endpoint", "REUSE")
|
|
||||||
s.assertJsonResponse(http.MethodPost, "reuse_backend/endpoint", "REUSE")
|
s.assertJsonResponse(http.MethodPost, "reuse_backend/endpoint", "REUSE")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,24 +173,23 @@ func (s *AcceptanceTestSuite) TestGrpcAliveEndpoint() {
|
|||||||
|
|
||||||
func (s *AcceptanceTestSuite) TestGrpcAliveApiEndpoint() {
|
func (s *AcceptanceTestSuite) TestGrpcAliveApiEndpoint() {
|
||||||
s.assertJsonResponse(http.MethodPost, "api/grpc/alive_backend/endpoint_v2", "GRPC_API")
|
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() {
|
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)
|
s.assertStatus(http.MethodPost, "grpc/alive_backend/endpoint_with_error", http.StatusPreconditionFailed)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *AcceptanceTestSuite) TestHttpAliveEndpoint() {
|
func (s *AcceptanceTestSuite) TestHttpAliveEndpoint() {
|
||||||
s.assertJsonResponse(http.MethodGet, "/api/alive_backend/endpoint", "HTTP_OK")
|
|
||||||
s.assertJsonResponse(http.MethodGet, "/alive_backend/endpoint", "HTTP_OK")
|
s.assertJsonResponse(http.MethodGet, "/alive_backend/endpoint", "HTTP_OK")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *AcceptanceTestSuite) TestHttpAliveApiEndpoint() {
|
func (s *AcceptanceTestSuite) TestHttpAliveApiEndpoint() {
|
||||||
s.assertJsonResponse(http.MethodGet, "/api/alive_backend/endpoint_v2", "HTTP_API")
|
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() {
|
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)
|
s.assertStatus(http.MethodPost, "/alive_backend/endpoint_with_error", http.StatusPreconditionFailed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user