From c938a10e9b87d1cf5d06c16efc5d40939f5fa865 Mon Sep 17 00:00:00 2001 From: Biju Kunjummen Date: Wed, 1 Nov 2017 08:54:36 -0700 Subject: [PATCH] Moves kotlin dsl to gateway-core (#96) Fixes gh-95 --- pom.xml | 2 +- spring-cloud-gateway-core/pom.xml | 83 +++++++++++++++++- .../cloud/gateway/route/GatewayDsl.kt | 56 ++++++++++++ .../cloud/gateway/route/GatewayDslTests.kt | 57 +++++++++++++ .../pom.xml | 85 ------------------- .../cloud/gateway/route/GatewayDsl.kt | 56 ------------ .../cloud/gateway/route/GatewayDslTests.kt | 57 ------------- 7 files changed, 196 insertions(+), 200 deletions(-) create mode 100644 spring-cloud-gateway-core/src/main/kotlin/org/springframework/cloud/gateway/route/GatewayDsl.kt create mode 100644 spring-cloud-gateway-core/src/test/kotlin/org/springframework/cloud/gateway/route/GatewayDslTests.kt delete mode 100644 spring-cloud-gateway-kotlin-extensions/pom.xml delete mode 100644 spring-cloud-gateway-kotlin-extensions/src/main/kotlin/org/springframework/cloud/gateway/route/GatewayDsl.kt delete mode 100644 spring-cloud-gateway-kotlin-extensions/src/test/kotlin/org/springframework/cloud/gateway/route/GatewayDslTests.kt diff --git a/pom.xml b/pom.xml index 39803e64..d94bedff 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,7 @@ 2.0.0.BUILD-SNAPSHOT 2.0.0.BUILD-SNAPSHOT 1.0.0.RELEASE + 1.1.51 @@ -136,7 +137,6 @@ spring-cloud-gateway-core spring-cloud-starter-gateway spring-cloud-gateway-sample - spring-cloud-gateway-kotlin-extensions docs diff --git a/spring-cloud-gateway-core/pom.xml b/spring-cloud-gateway-core/pom.xml index 883ee9c2..487160c6 100644 --- a/spring-cloud-gateway-core/pom.xml +++ b/spring-cloud-gateway-core/pom.xml @@ -66,6 +66,18 @@ spring-boot-starter-security true + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + true + + + org.jetbrains.kotlin + kotlin-reflect + ${kotlin.version} + true + org.springframework.cloud spring-cloud-starter-netflix-eureka-client @@ -92,6 +104,76 @@ test + + + + + kotlin-maven-plugin + org.jetbrains.kotlin + ${kotlin.version} + + 1.8 + + + + compile + compile + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/main/java + + + + + test-compile + test-compile + + + ${project.basedir}/src/test/kotlin + ${project.basedir}/src/test/java + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + -parameters + + + + + + default-compile + none + + + + default-testCompile + none + + + java-compile + compile + + compile + + + + java-test-compile + test-compile + + testCompile + + + + + + java8plus @@ -103,7 +185,6 @@ org.apache.maven.plugins maven-compiler-plugin - 3.6.0 -parameters 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 new file mode 100644 index 00000000..c48c4c9f --- /dev/null +++ b/spring-cloud-gateway-core/src/main/kotlin/org/springframework/cloud/gateway/route/GatewayDsl.kt @@ -0,0 +1,56 @@ +package org.springframework.cloud.gateway.route + +import reactor.core.publisher.Flux +import java.util.function.Predicate + +/** + * A Kotlin based DSL to configure a [RouteLocator] + * + * Example: + * ``` + * val routeLocator = gateway { + * route { + * id("test") + * uri("http://httpbin.org:80") + * predicate(host("**.abc.org") and path("/image/png")) + * add(addResponseHeader("X-TestHeader", "foobar")) + * } + * } + * ``` + * + * @author Biju Kunjummen + */ +fun gateway(routeLocator: RouteLocatorDsl.() -> Unit) = RouteLocatorDsl().apply(routeLocator).build() + + +/** + * Provider for [RouteLocator] DSL functionality + */ +class RouteLocatorDsl { + private val routes = mutableListOf() + + /** + * DSL to add a route to the [RouteLocator] + * + * @see [Route.Builder] + */ + fun route(init: Route.Builder.() -> Unit) { + routes += Route.builder().apply(init).build() + } + + fun build(): RouteLocator { + return RouteLocator { Flux.fromIterable(this.routes) } + } + + /** + * A helper to return a composed [Predicate] that tests against this [Predicate] AND the [other] predicate + */ + infix fun Predicate.and(other: Predicate) = this.and(other) + + /** + * A helper to return a composed [Predicate] that tests against this [Predicate] OR the [other] predicate + */ + infix fun Predicate.or(other: Predicate) = this.or(other) +} + + 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 new file mode 100644 index 00000000..befe2256 --- /dev/null +++ b/spring-cloud-gateway-core/src/test/kotlin/org/springframework/cloud/gateway/route/GatewayDslTests.kt @@ -0,0 +1,57 @@ +package org.springframework.cloud.gateway.route + +import org.junit.Test +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.mock.http.server.reactive.MockServerHttpRequest +import org.springframework.mock.web.server.MockServerWebExchange +import org.springframework.web.server.ServerWebExchange +import reactor.test.StepVerifier +import java.net.URI + +class GatewayDslTests { + + @Test + fun testSampleRouteDsl() { + val routeLocator = gateway { + route { + id("test") + uri("http://httpbin.org:80") + predicate(host("**.abc.org") and path("/image/png")) + add(addResponseHeader("X-TestHeader", "foobar")) + } + + route { + id("test2") + uri("http://httpbin.org:80") + predicate(path("/image/webp") or path("/image/anotherone")) + add(addResponseHeader("X-AnotherHeader", "baz")) + add(addResponseHeader("X-AnotherHeader-2", "baz-2")) + } + } + + StepVerifier + .create(routeLocator.routes) + .expectNextMatches({ r -> + r.id == "test" && r.filters.size == 1 && r.uri == URI.create("http://httpbin.org:80") + }) + .expectNextMatches({ r -> + r.id == "test2" && r.filters.size == 2 && r.uri == URI.create("http://httpbin.org:80") + }) + .expectComplete() + .verify() + + val sampleExchange: ServerWebExchange = MockServerWebExchange.from(MockServerHttpRequest.get("/image/webp") + .header("Host", "test.abc.org").build()) + + val filteredRoutes = routeLocator.routes.filter({ r -> r.predicate.test(sampleExchange) }) + + StepVerifier.create(filteredRoutes) + .expectNextMatches({ r -> + r.id == "test2" && r.filters.size == 2 && r.uri == URI.create("http://httpbin.org:80") + }) + .expectComplete() + .verify() + } +} \ No newline at end of file diff --git a/spring-cloud-gateway-kotlin-extensions/pom.xml b/spring-cloud-gateway-kotlin-extensions/pom.xml deleted file mode 100644 index a686095d..00000000 --- a/spring-cloud-gateway-kotlin-extensions/pom.xml +++ /dev/null @@ -1,85 +0,0 @@ - - - 4.0.0 - - - org.springframework.cloud - spring-cloud-gateway - 2.0.0.BUILD-SNAPSHOT - .. - - spring-cloud-gateway-kotlin-extensions - jar - Spring Cloud Gateway Kotlin Extensions - Spring Cloud Gateway Kotlin Extensions - - ${basedir}/.. - 1.1.4-3 - - - - - org.springframework.cloud - spring-cloud-gateway-core - - - org.jetbrains.kotlin - kotlin-stdlib-jre8 - ${kotlin.version} - - - org.springframework.boot - spring-boot-starter-webflux - true - - - org.springframework.boot - spring-boot-starter-test - test - - - io.projectreactor - reactor-test - test - - - org.assertj - assertj-core - test - - - - - src/main/kotlin - src/test/kotlin - - - kotlin-maven-plugin - - 1.8 - - org.jetbrains.kotlin - ${kotlin.version} - - - - compile - compile - - compile - - - - - test-compile - test-compile - - test-compile - - - - - - - diff --git a/spring-cloud-gateway-kotlin-extensions/src/main/kotlin/org/springframework/cloud/gateway/route/GatewayDsl.kt b/spring-cloud-gateway-kotlin-extensions/src/main/kotlin/org/springframework/cloud/gateway/route/GatewayDsl.kt deleted file mode 100644 index 39ba4c71..00000000 --- a/spring-cloud-gateway-kotlin-extensions/src/main/kotlin/org/springframework/cloud/gateway/route/GatewayDsl.kt +++ /dev/null @@ -1,56 +0,0 @@ -package org.springframework.cloud.gateway.route - -import reactor.core.publisher.Flux -import java.util.function.Predicate - -/** - * A Kotlin based DSL to configure a [RouteLocator] - * - * Example: - * ``` - * val routeLocator = gateway { - * route { - * id("test") - * uri("http://httpbin.org:80") - * predicate(host("**.abc.org") and path("/image/png")) - * add(addResponseHeader("X-TestHeader", "foobar")) - * } - * } - * ``` - * - * @author Biju Kunjummen - */ -fun gateway(routeLocator: RouteLocatorDsl.() -> Unit) = RouteLocatorDsl().apply(routeLocator).build() - - -/** - * Provider for [RouteLocator] DSL functionality - */ -class RouteLocatorDsl { - private val routes = mutableListOf() - - /** - * DSL to add a route to the [RouteLocator] - * - * @see [Route.Builder] - */ - fun route(init: Route.Builder.() -> Unit) { - routes += Route.builder().apply(init).build() - } - - fun build(): RouteLocator { - return RouteLocator { Flux.fromIterable(this.routes) } - } - - /** - * A helper to return a composed [Predicate] that tests against this [Predicate] AND the [other] predicate - */ - infix fun Predicate.and(other: Predicate) = this.and(other) - - /** - * A helper to return a composed [Predicate] that tests against this [Predicate] OR the [other] predicate - */ - infix fun Predicate.or(other: Predicate) = this.or(other) -} - - diff --git a/spring-cloud-gateway-kotlin-extensions/src/test/kotlin/org/springframework/cloud/gateway/route/GatewayDslTests.kt b/spring-cloud-gateway-kotlin-extensions/src/test/kotlin/org/springframework/cloud/gateway/route/GatewayDslTests.kt deleted file mode 100644 index e2bc1da2..00000000 --- a/spring-cloud-gateway-kotlin-extensions/src/test/kotlin/org/springframework/cloud/gateway/route/GatewayDslTests.kt +++ /dev/null @@ -1,57 +0,0 @@ -package org.springframework.cloud.gateway.route - -import org.junit.Test -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.mock.http.server.reactive.MockServerHttpRequest -import org.springframework.mock.web.server.MockServerWebExchange -import org.springframework.web.server.ServerWebExchange -import reactor.test.StepVerifier -import java.net.URI - -class GatewayDslTests { - - @Test - fun testSampleRouteDsl() { - val routeLocator = gateway { - route { - id("test") - uri("http://httpbin.org:80") - predicate(host("**.abc.org") and path("/image/png")) - add(addResponseHeader("X-TestHeader", "foobar")) - } - - route { - id("test2") - uri("http://httpbin.org:80") - predicate(path("/image/webp") or path("/image/anotherone")) - add(addResponseHeader("X-AnotherHeader", "baz")) - add(addResponseHeader("X-AnotherHeader-2", "baz-2")) - } - } - - StepVerifier - .create(routeLocator.routes) - .expectNextMatches({ r -> - r.id == "test" && r.filters.size == 1 && r.uri == URI.create("http://httpbin.org:80") - }) - .expectNextMatches({ r -> - r.id == "test2" && r.filters.size == 2 && r.uri == URI.create("http://httpbin.org:80") - }) - .expectComplete() - .verify() - - val sampleExchange: ServerWebExchange = MockServerWebExchange.from(MockServerHttpRequest.get("/image/webp") - .header("Host", "test.abc.org").build()) - - val filteredRoutes = routeLocator.routes.filter({ r -> r.predicate.test(sampleExchange) }) - - StepVerifier.create(filteredRoutes) - .expectNextMatches({ r -> - r.id == "test2" && r.filters.size == 2 && r.uri == URI.create("http://httpbin.org:80") - }) - .expectComplete() - .verify() - } -} \ No newline at end of file