-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Runtime app routing now uses a static route definition and rewrites t…
…he to forward in a filter
- Loading branch information
Showing
13 changed files
with
430 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/contentgrid/gateway/runtime/routing/RuntimeDeploymentGatewayFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.contentgrid.gateway.runtime.routing; | ||
|
||
import static com.contentgrid.gateway.runtime.web.ContentGridAppRequestWebFilter.CONTENTGRID_SERVICE_INSTANCE_ATTR; | ||
import static org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter.LOAD_BALANCER_CLIENT_FILTER_ORDER; | ||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR; | ||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR; | ||
|
||
import java.net.URI; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.client.loadbalancer.CompletionContext; | ||
import org.springframework.cloud.client.loadbalancer.LoadBalancerUriTools; | ||
import org.springframework.cloud.gateway.filter.GatewayFilterChain; | ||
import org.springframework.cloud.gateway.filter.GlobalFilter; | ||
import org.springframework.cloud.gateway.support.DelegatingServiceInstance; | ||
import org.springframework.cloud.gateway.support.NotFoundException; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class RuntimeDeploymentGatewayFilter implements GlobalFilter, Ordered { | ||
|
||
@Override | ||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { | ||
|
||
URI routedUri = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR); | ||
if (routedUri == null || !"cg".equals(routedUri.getScheme())) { | ||
return chain.filter(exchange); | ||
} | ||
|
||
ServiceInstance serviceInstance = exchange.getAttribute(CONTENTGRID_SERVICE_INSTANCE_ATTR); | ||
if (serviceInstance == null) { | ||
var host = exchange.getRequest().getURI().getHost(); | ||
var message = "Unable to find service instance for %s".formatted(host); | ||
throw NotFoundException.create(false /* HTTP 503 */, message); | ||
} else { | ||
|
||
// the `<scheme>` for routedUri is `cg://`, so we need to override the default scheme | ||
// if the serviceInstance doesn't provide one. | ||
String overrideScheme = serviceInstance.isSecure() ? "https" : "http"; | ||
serviceInstance = new DelegatingServiceInstance(serviceInstance, overrideScheme); | ||
|
||
routedUri = LoadBalancerUriTools.reconstructURI(serviceInstance, routedUri); | ||
|
||
if (log.isDebugEnabled()) { | ||
log.debug("Routing {} to {}", exchange.getRequest().getURI(), serviceInstance.getHost()); | ||
} | ||
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, routedUri); | ||
} | ||
|
||
return chain.filter(exchange); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return LOAD_BALANCER_CLIENT_FILTER_ORDER; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.