Skip to content

Commit

Permalink
Added proxy endpoint to UI service (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
niallthomson authored Jan 9, 2024
1 parent 67e7cd2 commit 0918817
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package com.amazon.sample.orders.web;

import com.amazon.sample.orders.entities.OrderEntity;
import com.amazon.sample.orders.services.OrderService;
import com.amazon.sample.orders.web.payload.*;

Expand All @@ -33,7 +32,7 @@

@RestController
@RequestMapping("/orders")
@Tag(name="orders")
@Tag(name = "orders")
@Slf4j
public class OrderController {

Expand Down
5 changes: 5 additions & 0 deletions src/ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway-webflux</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package com.amazon.sample.ui.services.checkout;

import com.amazon.sample.ui.services.checkout.model.Checkout;
import com.amazon.sample.ui.services.checkout.model.CheckoutSubmitted;
import com.amazon.sample.ui.services.checkout.model.CheckoutSubmittedResponse;
import com.amazon.sample.ui.services.checkout.model.ShippingAddress;
import reactor.core.publisher.Mono;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@

package com.amazon.sample.ui.web;

import com.amazon.sample.ui.clients.carts.api.CartsApi;
import com.amazon.sample.ui.services.Metadata;
import com.amazon.sample.ui.services.carts.CartsService;
import com.amazon.sample.ui.web.util.SessionIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.ui.Model;
import org.springframework.web.server.ServerWebExchange;
import reactor.util.retry.Retry;
import reactor.util.retry.RetryBackoffSpec;

Expand All @@ -49,8 +47,8 @@ public BaseController(CartsService cartsService, Metadata metadata) {

protected static RetryBackoffSpec retrySpec(String path) {
return Retry
.backoff(3, Duration.ofSeconds(1))
.doBeforeRetry(context -> log.warn("Retrying {}", path));
.backoff(3, Duration.ofSeconds(1))
.doBeforeRetry(context -> log.warn("Retrying {}", path));
}

protected void populateCommon(ServerHttpRequest request, Model model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,28 @@
package com.amazon.sample.ui.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;

import com.amazon.sample.ui.services.assets.AssetsService;

import reactor.core.publisher.Mono;

import java.util.concurrent.TimeUnit;

/**
* This controller serves product images from the catalog backend, adding cache control headers along the way
* This controller serves product images from the catalog backend, adding cache
* control headers along the way
*/
@RestController
public class CatalogImageController {

private AssetsService assetsService;
private AssetsService assetsService;

public CatalogImageController(@Autowired AssetsService assetsService) {
this.assetsService = assetsService;
}
public CatalogImageController(@Autowired AssetsService assetsService) {
this.assetsService = assetsService;
}

@GetMapping(value = "/assets/{image}", produces = MediaType.IMAGE_JPEG_VALUE)
public Mono<ResponseEntity<byte[]>> catalogueImage(@PathVariable String image) {
Expand Down
83 changes: 83 additions & 0 deletions src/ui/src/main/java/com/amazon/sample/ui/web/ProxyController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT-0
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.amazon.sample.ui.web;

import reactor.core.publisher.Mono;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.webflux.ProxyExchange;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/proxy")
public class ProxyController {

@Value("${endpoints.catalog:}")
private String catalogEndpoint;

@Value("${endpoints.carts:}")
private String cartsEndpoint;

@Value("${endpoints.orders:}")
private String ordersEndpoint;

@Value("${endpoints.checkout:}")
private String checkoutEndpoint;

@GetMapping("/catalogue/**")
public Mono<ResponseEntity<byte[]>> catalogProxy(ProxyExchange<byte[]> proxy) throws Exception {
return doProxy(proxy, "catalog", catalogEndpoint);
}

@GetMapping("/carts/**")
public Mono<ResponseEntity<byte[]>> cartsProxy(ProxyExchange<byte[]> proxy) throws Exception {
return doProxy(proxy, "carts", cartsEndpoint);
}

@GetMapping("/checkout/**")
public Mono<ResponseEntity<byte[]>> checkoutProxy(ProxyExchange<byte[]> proxy) throws Exception {
return doProxy(proxy, "checkout", checkoutEndpoint);
}

@GetMapping("/orders/**")
public Mono<ResponseEntity<byte[]>> ordersProxy(ProxyExchange<byte[]> proxy) throws Exception {
return doProxy(proxy, "orders", checkoutEndpoint);
}

public Mono<ResponseEntity<byte[]>> doProxy(ProxyExchange<byte[]> proxy, String service, String endpoint)
throws Exception {
System.out.println("Endpoint is " + endpoint);
if (isEmpty(endpoint)) {
return Mono
.just(new ResponseEntity<>(("Endpoint not provided for " + service).getBytes(),
HttpStatus.NOT_FOUND));
}

String path = proxy.path("/proxy");
return proxy.uri(endpoint + path).header("Content-Type", "application/json").forward();
}

private boolean isEmpty(String check) {
return check.equals("false");
}
}

0 comments on commit 0918817

Please sign in to comment.