From b2e55b3fdcccfabd5809fcf4a5948a5bb089ccfb Mon Sep 17 00:00:00 2001 From: Biju Kunjummen Date: Thu, 16 Nov 2017 11:16:26 -0600 Subject: [PATCH] Add support for select function paramters for gateway kotlin dsl (#98) --- .../cloud/gateway/route/GatewayDsl.kt | 28 ++++- .../cloud/gateway/route/GatewayDslTests.kt | 41 ++++++- spring-cloud-gateway-sample/pom.xml | 102 +++++++++++++++++- .../sample/GatewaySampleApplication.java | 4 + .../cloud/gateway/sample/AdditionalRoutes.kt | 23 ++++ 5 files changed, 191 insertions(+), 7 deletions(-) create mode 100644 spring-cloud-gateway-sample/src/main/kotlin/org/springframework/cloud/gateway/sample/AdditionalRoutes.kt diff --git a/spring-cloud-gateway-core/src/main/kotlin/org/springframework/cloud/gateway/route/GatewayDsl.kt b/spring-cloud-gateway-core/src/main/kotlin/org/springframework/cloud/gateway/route/GatewayDsl.kt index c48c4c9f1..f8e9e0e34 100644 --- a/spring-cloud-gateway-core/src/main/kotlin/org/springframework/cloud/gateway/route/GatewayDsl.kt +++ b/spring-cloud-gateway-core/src/main/kotlin/org/springframework/cloud/gateway/route/GatewayDsl.kt @@ -1,3 +1,20 @@ +/* + * Copyright 2013-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + package org.springframework.cloud.gateway.route import reactor.core.publisher.Flux @@ -9,8 +26,7 @@ import java.util.function.Predicate * Example: * ``` * val routeLocator = gateway { - * route { - * id("test") + * route(id = "test") { * uri("http://httpbin.org:80") * predicate(host("**.abc.org") and path("/image/png")) * add(addResponseHeader("X-TestHeader", "foobar")) @@ -34,8 +50,12 @@ class RouteLocatorDsl { * * @see [Route.Builder] */ - fun route(init: Route.Builder.() -> Unit) { - routes += Route.builder().apply(init).build() + fun route(id: String? = null, order: Int = 0, uri: String? = null, init: Route.Builder.() -> Unit) { + val builder = Route.builder() + if (uri != null) { + builder.uri(uri) + } + routes += builder.id(id).order(order).apply(init).build() } fun build(): RouteLocator { diff --git a/spring-cloud-gateway-core/src/test/kotlin/org/springframework/cloud/gateway/route/GatewayDslTests.kt b/spring-cloud-gateway-core/src/test/kotlin/org/springframework/cloud/gateway/route/GatewayDslTests.kt index befe2256e..956805f10 100644 --- a/spring-cloud-gateway-core/src/test/kotlin/org/springframework/cloud/gateway/route/GatewayDslTests.kt +++ b/spring-cloud-gateway-core/src/test/kotlin/org/springframework/cloud/gateway/route/GatewayDslTests.kt @@ -1,3 +1,20 @@ +/* + * Copyright 2013-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + package org.springframework.cloud.gateway.route import org.junit.Test @@ -13,7 +30,7 @@ import java.net.URI class GatewayDslTests { @Test - fun testSampleRouteDsl() { + fun sampleRouteDsl() { val routeLocator = gateway { route { id("test") @@ -54,4 +71,26 @@ class GatewayDslTests { .expectComplete() .verify() } + + @Test + fun dslWithFunctionParameters() { + val routerLocator = gateway { + route(id = "test", order = 10, uri = "http://httpbin.org") { + predicate(host("**.abc.org")) + } + } + + StepVerifier.create(routerLocator.routes) + .expectNextMatches({ r -> + r.id == "test" && + r.uri == URI.create("http://httpbin.org") && + r.order == 10 && + r.order == 10 && + r.predicate.test(MockServerWebExchange + .from(MockServerHttpRequest + .get("/someuri").header("Host", "test.abc.org"))) + }) + .expectComplete() + .verify() + } } \ No newline at end of file diff --git a/spring-cloud-gateway-sample/pom.xml b/spring-cloud-gateway-sample/pom.xml index 297d53299..fc7280ed4 100644 --- a/spring-cloud-gateway-sample/pom.xml +++ b/spring-cloud-gateway-sample/pom.xml @@ -1,6 +1,7 @@ - + 4.0.0 org.springframework.cloud.gateway @@ -10,6 +11,10 @@ Spring Cloud Gateway Sample Spring Cloud Gateway Sample + + 1.1.51 + + org.springframework.cloud spring-cloud-gateway @@ -50,9 +55,102 @@ assertj-core test + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + true + + + org.jetbrains.kotlin + kotlin-reflect + ${kotlin.version} + true + + + kotlin-maven-plugin + org.jetbrains.kotlin + ${kotlin.version} + + + spring + + 1.8 + + + + compile + compile + + compile + + + + src/main/java + src/main/kotlin + + + + + test-compile + test-compile + + test-compile + + + + src/test/java + src/test/kotlin + + + + + + + org.jetbrains.kotlin + kotlin-maven-allopen + ${kotlin.version} + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + -parameters + + + + + + default-compile + none + + + + default-testCompile + none + + + java-compile + compile + + compile + + + + java-test-compile + test-compile + + testCompile + + + + maven-deploy-plugin diff --git a/spring-cloud-gateway-sample/src/main/java/org/springframework/cloud/gateway/sample/GatewaySampleApplication.java b/spring-cloud-gateway-sample/src/main/java/org/springframework/cloud/gateway/sample/GatewaySampleApplication.java index 528c3c918..15acdb59a 100644 --- a/spring-cloud-gateway-sample/src/main/java/org/springframework/cloud/gateway/sample/GatewaySampleApplication.java +++ b/spring-cloud-gateway-sample/src/main/java/org/springframework/cloud/gateway/sample/GatewaySampleApplication.java @@ -23,6 +23,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.Routes; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RouterFunction; @@ -39,10 +40,12 @@ import static org.springframework.tuple.TupleBuilder.tuple; */ @SpringBootConfiguration @EnableAutoConfiguration +@Import(AdditionalRoutes.class) public class GatewaySampleApplication { @Bean public RouteLocator customRouteLocator(ThrottleGatewayFilterFactory throttle) { + //@formatter:off return Routes.locator() .route("test") .predicate(host("**.abc.org").and(path("/image/png"))) @@ -61,6 +64,7 @@ public class GatewaySampleApplication { "refillUnit", "SECONDS"))) .uri("http://httpbin.org:80") .build(); + ////@formatter:on } @Bean diff --git a/spring-cloud-gateway-sample/src/main/kotlin/org/springframework/cloud/gateway/sample/AdditionalRoutes.kt b/spring-cloud-gateway-sample/src/main/kotlin/org/springframework/cloud/gateway/sample/AdditionalRoutes.kt new file mode 100644 index 000000000..3e35379e9 --- /dev/null +++ b/spring-cloud-gateway-sample/src/main/kotlin/org/springframework/cloud/gateway/sample/AdditionalRoutes.kt @@ -0,0 +1,23 @@ +package org.springframework.cloud.gateway.sample + +import org.springframework.cloud.gateway.filter.factory.GatewayFilters.addResponseHeader +import org.springframework.cloud.gateway.handler.predicate.RoutePredicates.host +import org.springframework.cloud.gateway.handler.predicate.RoutePredicates.path +import org.springframework.cloud.gateway.route.RouteLocator +import org.springframework.cloud.gateway.route.gateway +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + +@Configuration +class AdditionalRoutes { + + @Bean + fun additionalRouteLocator(): RouteLocator = gateway { + route(id = "test-kotlin") { + uri("http://httpbin.org:80") + predicate(host("kotlin.abc.org") and path("/image/png")) + add(addResponseHeader("X-TestHeader", "foobar")) + } + } + +} \ No newline at end of file