Browse Source

Updates to RouteLocatorBuild API

pull/127/head
Spencer Gibb 7 years ago
parent
commit
83496b7894
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 18
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/builder/RouteLocatorBuilder.java
  2. 9
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/PrincipalNameKeyResolverIntegrationTests.java
  3. 5
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/websocket/WebSocketIntegrationTests.java

18
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/builder/RouteLocatorBuilder.java

@ -19,6 +19,8 @@ package org.springframework.cloud.gateway.route.builder; @@ -19,6 +19,8 @@ package org.springframework.cloud.gateway.route.builder;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
@ -47,8 +49,12 @@ public class RouteLocatorBuilder { @@ -47,8 +49,12 @@ public class RouteLocatorBuilder {
this.context = context;
}
public PredicateSpec route(String id) {
return new RouteSpec(this).id(id);
public Builder route(String id, Function<PredicateSpec, Builder> fn) {
return fn.apply(new RouteSpec(this).id(id));
}
public Builder route(Function<PredicateSpec, Builder> fn) {
return fn.apply(new RouteSpec(this).randomId());
}
private void add(Route route) {
@ -57,13 +63,13 @@ public class RouteLocatorBuilder { @@ -57,13 +63,13 @@ public class RouteLocatorBuilder {
Builder uri(Route.Builder builder, String uri) {
Route route = builder.uri(uri).build();
routes.add(route);
add(route);
return this;
}
Builder uri(Route.Builder builder, URI uri) {
Route route = builder.uri(uri).build();
routes.add(route);
add(route);
return this;
}
@ -90,6 +96,10 @@ public class RouteLocatorBuilder { @@ -90,6 +96,10 @@ public class RouteLocatorBuilder {
return predicateBuilder();
}
public PredicateSpec randomId() {
return id(UUID.randomUUID().toString());
}
private PredicateSpec predicateBuilder() {
return new PredicateSpec(this.routeBuilder, this.builder);
}

9
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/PrincipalNameKeyResolverIntegrationTests.java

@ -93,11 +93,10 @@ public class PrincipalNameKeyResolverIntegrationTests { @@ -93,11 +93,10 @@ public class PrincipalNameKeyResolverIntegrationTests {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("protected-throttled")
.path("/myapi/**")
.requestRateLimiter(tuple().build())
.prefixPath("/downstream")
.uri("http://localhost:"+port)
.route(r -> r.path("/myapi/**")
.requestRateLimiter(tuple().build())
.prefixPath("/downstream")
.uri("http://localhost:"+port))
.build();
}

5
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/websocket/WebSocketIntegrationTests.java

@ -303,9 +303,8 @@ public class WebSocketIntegrationTests { @@ -303,9 +303,8 @@ public class WebSocketIntegrationTests {
@Bean
public RouteLocator wsRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("testws")
.alwaysTrue()
.uri("ws://localhost:"+this.wsPort)
.route(r -> r.alwaysTrue()
.uri("ws://localhost:"+this.wsPort))
.build();
}
}

Loading…
Cancel
Save