8 changed files with 224 additions and 36 deletions
@ -0,0 +1,54 @@
@@ -0,0 +1,54 @@
|
||||
package org.springframework.cloud.gateway; |
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
||||
import org.springframework.cloud.gateway.filters.FindRouteFilter; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector; |
||||
import org.springframework.web.client.reactive.WebClient; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
@Configuration |
||||
@EnableConfigurationProperties |
||||
public class GatewayConfiguration { |
||||
|
||||
@Bean |
||||
@ConditionalOnMissingBean |
||||
public WebClient webClient() { |
||||
return WebClient.builder(new ReactorClientHttpConnector()).build(); |
||||
} |
||||
|
||||
@Bean |
||||
public FindRouteFilter findRouteFilter(GatewayProperties properties) { |
||||
return new FindRouteFilter(properties); |
||||
} |
||||
|
||||
@Bean |
||||
public GatewayProperties gatewayProperties() { |
||||
return new GatewayProperties(); |
||||
} |
||||
|
||||
@Bean |
||||
public GatewayWebHandler gatewayController(GatewayProperties properties, WebClient webClient) { |
||||
return new GatewayWebHandler(properties, webClient); |
||||
} |
||||
|
||||
@Bean |
||||
public GatewayHandlerMapping gatewayHandlerMapping(GatewayProperties properties, GatewayWebHandler gatewayWebHandler) { |
||||
return new GatewayHandlerMapping(properties, gatewayWebHandler); |
||||
} |
||||
|
||||
/*@Bean |
||||
public GatewayWebReactiveConfigurer gatewayWebReactiveConfiguration() { |
||||
return new GatewayWebReactiveConfigurer(); |
||||
} |
||||
|
||||
public static class GatewayWebReactiveConfigurer implements WebReactiveConfigurer { |
||||
@Override |
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) { |
||||
} |
||||
}*/ |
||||
} |
@ -0,0 +1,39 @@
@@ -0,0 +1,39 @@
|
||||
package org.springframework.cloud.gateway; |
||||
|
||||
import org.springframework.beans.BeansException; |
||||
import org.springframework.cloud.gateway.GatewayProperties.Route; |
||||
import org.springframework.web.reactive.handler.AbstractUrlHandlerMapping; |
||||
import org.springframework.web.server.ServerWebExchange; |
||||
|
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
public class GatewayHandlerMapping extends AbstractUrlHandlerMapping { |
||||
|
||||
private GatewayProperties properties; |
||||
private GatewayWebHandler gatewayWebHandler; |
||||
|
||||
public GatewayHandlerMapping(GatewayProperties properties, GatewayWebHandler gatewayWebHandler) { |
||||
this.properties = properties; |
||||
this.gatewayWebHandler = gatewayWebHandler; |
||||
} |
||||
|
||||
@Override |
||||
protected void initApplicationContext() throws BeansException { |
||||
super.initApplicationContext(); |
||||
registerHandlers(this.properties.getRoutes()); |
||||
} |
||||
|
||||
protected void registerHandlers(Map<String, Route> routes) { |
||||
for (Route route : routes.values()) { |
||||
registerHandler(route.getPath(), this.gatewayWebHandler); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected Object lookupHandler(String urlPath, ServerWebExchange exchange) throws Exception { |
||||
return super.lookupHandler(urlPath, exchange); |
||||
} |
||||
} |
@ -0,0 +1,49 @@
@@ -0,0 +1,49 @@
|
||||
package org.springframework.cloud.gateway; |
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
|
||||
import java.util.LinkedHashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
@ConfigurationProperties("spring.cloud.gateway") |
||||
public class GatewayProperties { |
||||
|
||||
|
||||
/** |
||||
* Map of route names to properties. |
||||
*/ |
||||
private Map<String, Route> routes = new LinkedHashMap<>(); |
||||
|
||||
public Map<String, Route> getRoutes() { |
||||
return routes; |
||||
} |
||||
|
||||
public void setRoutes(Map<String, Route> routes) { |
||||
this.routes = routes; |
||||
} |
||||
|
||||
public static class Route { |
||||
private String id; |
||||
private String path; |
||||
private String url; |
||||
|
||||
public String getPath() { |
||||
return this.path; |
||||
} |
||||
|
||||
public void setPath(String path) { |
||||
this.path = path; |
||||
} |
||||
|
||||
public String getUrl() { |
||||
return this.url; |
||||
} |
||||
|
||||
public void setUrl(String url) { |
||||
this.url = url; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
package org.springframework.cloud.gateway.filters; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
import org.springframework.cloud.gateway.GatewayProperties; |
||||
import org.springframework.cloud.gateway.GatewayProperties.Route; |
||||
import org.springframework.cloud.gateway.SpringCloudGatewayApplication; |
||||
import org.springframework.http.server.reactive.ServerHttpRequest; |
||||
import org.springframework.util.AntPathMatcher; |
||||
import org.springframework.web.server.ServerWebExchange; |
||||
import org.springframework.web.server.WebFilter; |
||||
import org.springframework.web.server.WebFilterChain; |
||||
import reactor.core.publisher.Mono; |
||||
|
||||
import java.net.URI; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
public class FindRouteFilter implements WebFilter { |
||||
|
||||
private static final Log log = LogFactory.getLog(SpringCloudGatewayApplication.class); |
||||
|
||||
private final GatewayProperties properties; |
||||
private final AntPathMatcher matcher; |
||||
|
||||
public FindRouteFilter(GatewayProperties properties) { |
||||
this.properties = properties; |
||||
this.matcher = new AntPathMatcher(); |
||||
} |
||||
|
||||
@Override |
||||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { |
||||
log.info("FindRouteFilter start"); |
||||
//TODO:
|
||||
ServerHttpRequest request = exchange.getRequest(); |
||||
URI uri = request.getURI(); |
||||
String path = uri.getPath(); |
||||
for (Route route : this.properties.getRoutes().values()) { |
||||
if (this.matcher.match(route.getPath(), path)) { |
||||
String url = route.getUrl() + path; |
||||
exchange.getAttributes().put("requestUrl", url); |
||||
return chain.filter(exchange); |
||||
} |
||||
} |
||||
return chain.filter(exchange); |
||||
} |
||||
} |
Loading…
Reference in new issue