Browse Source

Expanded /gateway/routes endpoint.

pull/279/head
Spencer Gibb 7 years ago
parent
commit
fa6a7ff41f
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 44
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/actuate/GatewayControllerEndpoint.java
  2. 17
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/actuate/GatewayControllerEndpointTests.java

44
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/actuate/GatewayControllerEndpoint.java

@ -18,6 +18,7 @@ @@ -18,6 +18,7 @@
package org.springframework.cloud.gateway.actuate;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -25,6 +26,7 @@ import java.util.Map; @@ -25,6 +26,7 @@ import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.filter.factory.GatewayFilterFactory;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
@ -109,15 +111,45 @@ public class GatewayControllerEndpoint implements ApplicationEventPublisherAware @@ -109,15 +111,45 @@ public class GatewayControllerEndpoint implements ApplicationEventPublisherAware
return map;
}
// TODO: Add support for RouteLocator
// TODO: Flush out routes without a definition
@GetMapping("/routes")
public Mono<Map<String, List>> routes() {
Mono<List<RouteDefinition>> routeDefs = this.routeDefinitionLocator.getRouteDefinitions().collectList();
public Mono<List<Map<String, Object>>> routes() {
Mono<Map<String, RouteDefinition>> routeDefs = this.routeDefinitionLocator.getRouteDefinitions()
.collectMap(RouteDefinition::getId);
Mono<List<Route>> routes = this.routeLocator.getRoutes().collectList();
return Mono.zip(routeDefs, routes).map(tuple -> {
Map<String, List> allRoutes = new HashMap<>();
allRoutes.put("routeDefinitions", tuple.getT1());
// allRoutes.put("routes", tuple.getT2());
Map<String, RouteDefinition> defs = tuple.getT1();
List<Route> routeList = tuple.getT2();
List<Map<String, Object>> allRoutes = new ArrayList<>();
routeList.forEach(route -> {
HashMap<String, Object> r = new HashMap<>();
r.put("route_id", route.getId());
r.put("order", route.getOrder());
if (defs.containsKey(route.getId())) {
r.put("route_definition", defs.get(route.getId()));
} else {
HashMap<String, Object> obj = new HashMap<>();
obj.put("predicate", route.getPredicate().toString());
if (!route.getFilters().isEmpty()) {
ArrayList<String> filters = new ArrayList<>();
for (GatewayFilter filter : route.getFilters()) {
filters.add(filter.toString());
}
obj.put("filters", filters);
}
if (!obj.isEmpty()) {
r.put("route_object", obj);
}
}
allRoutes.add(r);
});
return allRoutes;
});
}

17
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/actuate/GatewayControllerEndpointTests.java

@ -17,6 +17,9 @@ @@ -17,6 +17,9 @@
package org.springframework.cloud.gateway.actuate;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -30,6 +33,7 @@ import org.springframework.context.annotation.Import; @@ -30,6 +33,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class)
@ -50,6 +54,19 @@ public class GatewayControllerEndpointTests { @@ -50,6 +54,19 @@ public class GatewayControllerEndpointTests {
.expectStatus().isOk();
}
@Test
public void testRoutes() {
testClient.get()
.uri("http://localhost:"+port+"/actuator/gateway/routes")
.exchange()
.expectStatus().isOk()
.expectBodyList(Map.class)
.consumeWith(result -> {
List<Map> responseBody = result.getResponseBody();
assertThat(responseBody).isNotEmpty();
});
}
@SpringBootConfiguration
@EnableAutoConfiguration
@Import(PermitAllSecurityConfiguration.class)

Loading…
Cancel
Save