From 43d47e6cccd3f263c49142e482b274ea04f8f1b1 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Fri, 1 Dec 2017 15:37:02 -0500 Subject: [PATCH] Updated routes dsl to deal with new routes java api. --- .../cloud/gateway/route/builder/RouteDsl.kt | 26 +++++++++---------- .../cloud/gateway/sample/AdditionalRoutes.kt | 15 ++++++----- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/spring-cloud-gateway-core/src/main/kotlin/org/springframework/cloud/gateway/route/builder/RouteDsl.kt b/spring-cloud-gateway-core/src/main/kotlin/org/springframework/cloud/gateway/route/builder/RouteDsl.kt index b9fb9189c..c5b413bb8 100644 --- a/spring-cloud-gateway-core/src/main/kotlin/org/springframework/cloud/gateway/route/builder/RouteDsl.kt +++ b/spring-cloud-gateway-core/src/main/kotlin/org/springframework/cloud/gateway/route/builder/RouteDsl.kt @@ -75,6 +75,19 @@ class RouteLocatorDsl(val builder: RouteLocatorBuilder) { return routes.build() } + /** + * A helper to return a composed [Predicate] that tests against this [Predicate] AND the [other] predicate + */ + infix fun BooleanSpec.and(other: BooleanSpec) = + this.routeBuilder.predicate(this.predicate.and(other.predicate)) + + /** + * A helper to return a composed [Predicate] that tests against this [Predicate] OR the [other] predicate + */ + infix fun BooleanSpec.or(other: BooleanSpec) = + this.routeBuilder.predicate(this.predicate.or(other.predicate)) + + } /** @@ -86,16 +99,3 @@ fun PredicateSpec.filters(init: GatewayFilterSpec.() -> Unit) { } -/** - * A helper to return a composed [Predicate] that tests against this [Predicate] AND the [other] predicate - */ -infix fun BooleanSpec.and(other: BooleanSpec) = - this.routeBuilder.predicate(this.predicate.and(other.predicate)) - -/** - * A helper to return a composed [Predicate] that tests against this [Predicate] OR the [other] predicate - */ -infix fun BooleanSpec.or(other: BooleanSpec) = - this.routeBuilder.predicate(this.predicate.or(other.predicate)) - - 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 index 3e35379e9..96ac87df9 100644 --- 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 @@ -1,10 +1,9 @@ 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.cloud.gateway.route.builder.RouteLocatorBuilder +import org.springframework.cloud.gateway.route.builder.filters +import org.springframework.cloud.gateway.route.builder.routes import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration @@ -12,11 +11,13 @@ import org.springframework.context.annotation.Configuration class AdditionalRoutes { @Bean - fun additionalRouteLocator(): RouteLocator = gateway { + fun additionalRouteLocator(builder: RouteLocatorBuilder): RouteLocator = builder.routes { route(id = "test-kotlin") { + host("kotlin.abc.org") and path("/image/png") + filters { + addResponseHeader("X-TestHeader", "foobar") + } uri("http://httpbin.org:80") - predicate(host("kotlin.abc.org") and path("/image/png")) - add(addResponseHeader("X-TestHeader", "foobar")) } }