From 21614831b779d537e8fb28ce4c0dc3cf70e7bba6 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 22 Nov 2016 19:07:33 -0700 Subject: [PATCH] attempt to add host mapping --- .../cloud/gateway/GatewayHandlerMapping.java | 9 +++--- .../cloud/gateway/GatewayProperties.java | 22 ++++++++++---- .../gateway/filters/FindRouteFilter.java | 30 +++++++++++++------ src/main/resources/application.yml | 3 +- 4 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/springframework/cloud/gateway/GatewayHandlerMapping.java b/src/main/java/org/springframework/cloud/gateway/GatewayHandlerMapping.java index 57b0cc53..bf1b5076 100644 --- a/src/main/java/org/springframework/cloud/gateway/GatewayHandlerMapping.java +++ b/src/main/java/org/springframework/cloud/gateway/GatewayHandlerMapping.java @@ -2,6 +2,7 @@ package org.springframework.cloud.gateway; import org.springframework.beans.BeansException; import org.springframework.cloud.gateway.GatewayProperties.Route; +import org.springframework.util.StringUtils; import org.springframework.web.reactive.handler.AbstractUrlHandlerMapping; import org.springframework.web.server.ServerWebExchange; @@ -28,12 +29,10 @@ public class GatewayHandlerMapping extends AbstractUrlHandlerMapping { protected void registerHandlers(Map routes) { for (Route route : routes.values()) { - registerHandler(route.getRequestPath(), this.gatewayWebHandler); + if (StringUtils.hasText(route.getRequestPath())) { + registerHandler(route.getRequestPath(), this.gatewayWebHandler); + } } } - @Override - protected Object lookupHandler(String urlPath, ServerWebExchange exchange) throws Exception { - return super.lookupHandler(urlPath, exchange); - } } diff --git a/src/main/java/org/springframework/cloud/gateway/GatewayProperties.java b/src/main/java/org/springframework/cloud/gateway/GatewayProperties.java index 0f3231c8..3c2d2e42 100644 --- a/src/main/java/org/springframework/cloud/gateway/GatewayProperties.java +++ b/src/main/java/org/springframework/cloud/gateway/GatewayProperties.java @@ -29,7 +29,8 @@ public class GatewayProperties { public static class Route { private String id; private String requestPath; - private URI upstreamUrl; + private String requestHost; + private URI downstreamUrl; public String getRequestPath() { return this.requestPath; @@ -39,12 +40,20 @@ public class GatewayProperties { this.requestPath = requestPath; } - public URI getUpstreamUrl() { - return upstreamUrl; + public String getRequestHost() { + return requestHost; } - public void setUpstreamUrl(URI upstreamUrl) { - this.upstreamUrl = upstreamUrl; + public void setRequestHost(String requestHost) { + this.requestHost = requestHost; + } + + public URI getDownstreamUrl() { + return downstreamUrl; + } + + public void setDownstreamUrl(URI downstreamUrl) { + this.downstreamUrl = downstreamUrl; } @Override @@ -52,7 +61,8 @@ public class GatewayProperties { return "Route{" + "id='" + id + '\'' + ", requestPath='" + requestPath + '\'' + - ", upstreamUrl='" + upstreamUrl + '\'' + + ", requestHost='" + requestHost + '\'' + + ", downstreamUrl='" + downstreamUrl + '\'' + '}'; } } diff --git a/src/main/java/org/springframework/cloud/gateway/filters/FindRouteFilter.java b/src/main/java/org/springframework/cloud/gateway/filters/FindRouteFilter.java index b82a5ac2..5b455726 100644 --- a/src/main/java/org/springframework/cloud/gateway/filters/FindRouteFilter.java +++ b/src/main/java/org/springframework/cloud/gateway/filters/FindRouteFilter.java @@ -16,6 +16,8 @@ import reactor.core.publisher.Mono; import java.net.URI; +import static org.springframework.util.StringUtils.hasText; + /** * @author Spencer Gibb */ @@ -24,11 +26,11 @@ public class FindRouteFilter implements WebFilter, Ordered { private static final Log log = LogFactory.getLog(GatewayApplication.class); private final GatewayProperties properties; - private final AntPathMatcher matcher; + private final AntPathMatcher pathMatcher = new AntPathMatcher(); + private final AntPathMatcher hostMatcher = new AntPathMatcher("."); public FindRouteFilter(GatewayProperties properties) { this.properties = properties; - this.matcher = new AntPathMatcher(); } @Override @@ -43,16 +45,26 @@ public class FindRouteFilter implements WebFilter, Ordered { ServerHttpRequest request = exchange.getRequest(); URI uri = request.getURI(); String path = uri.getPath(); + String host = uri.getHost(); for (Route route : this.properties.getRoutes().values()) { - if (this.matcher.match(route.getRequestPath(), path)) { - URI requestUrl = UriComponentsBuilder.fromHttpRequest(request) - .uri(route.getUpstreamUrl()) - .build(true) - .toUri(); - exchange.getAttributes().put("requestUrl", requestUrl); - return chain.filter(exchange); + if (hasText(route.getRequestPath()) + && this.pathMatcher.match(route.getRequestPath(), path)) { + populateRequestUrl(exchange, request, route); + // TODO: this stuff needs to move into GatewayHandlerMapping + // otherwise, only path based routing works + } else if (hasText(route.getRequestHost()) + && this.hostMatcher.match(route.getRequestHost(), host)) { + populateRequestUrl(exchange, request, route); } } return chain.filter(exchange); } + + private void populateRequestUrl(ServerWebExchange exchange, ServerHttpRequest request, Route route) { + URI requestUrl = UriComponentsBuilder.fromHttpRequest(request) + .uri(route.getDownstreamUrl()) + .build(true) + .toUri(); + exchange.getAttributes().put("requestUrl", requestUrl); + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 0d375ac9..9e73c77c 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -7,4 +7,5 @@ spring: routes: test1: requestPath: /** - upstreamUrl: http://httpbin.org:80 +# requestHost: '**.example.org' + downstreamUrl: http://httpbin.org:80