attempt to add host mapping

This commit is contained in:
Spencer Gibb
2016-11-22 19:07:33 -07:00
parent f0a8a412fa
commit 21614831b7
4 changed files with 43 additions and 21 deletions

View File

@@ -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<String, Route> 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);
}
}

View File

@@ -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 + '\'' +
'}';
}
}

View File

@@ -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);
}
}

View File

@@ -7,4 +7,5 @@ spring:
routes:
test1:
requestPath: /**
upstreamUrl: http://httpbin.org:80
# requestHost: '**.example.org'
downstreamUrl: http://httpbin.org:80