Browse Source

attempt to add host mapping

pull/41/head
Spencer Gibb 8 years ago
parent
commit
21614831b7
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 9
      src/main/java/org/springframework/cloud/gateway/GatewayHandlerMapping.java
  2. 22
      src/main/java/org/springframework/cloud/gateway/GatewayProperties.java
  3. 30
      src/main/java/org/springframework/cloud/gateway/filters/FindRouteFilter.java
  4. 3
      src/main/resources/application.yml

9
src/main/java/org/springframework/cloud/gateway/GatewayHandlerMapping.java

@ -2,6 +2,7 @@ package org.springframework.cloud.gateway; @@ -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 { @@ -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);
}
}

22
src/main/java/org/springframework/cloud/gateway/GatewayProperties.java

@ -29,7 +29,8 @@ public class GatewayProperties { @@ -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 { @@ -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 { @@ -52,7 +61,8 @@ public class GatewayProperties {
return "Route{" +
"id='" + id + '\'' +
", requestPath='" + requestPath + '\'' +
", upstreamUrl='" + upstreamUrl + '\'' +
", requestHost='" + requestHost + '\'' +
", downstreamUrl='" + downstreamUrl + '\'' +
'}';
}
}

30
src/main/java/org/springframework/cloud/gateway/filters/FindRouteFilter.java

@ -16,6 +16,8 @@ import reactor.core.publisher.Mono; @@ -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 { @@ -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 { @@ -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);
}
}

3
src/main/resources/application.yml

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

Loading…
Cancel
Save