Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#54: swagger ui is not available for api-gateway. Fixed. Added tests … #55

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ You can also run the Postgres version using `./gradlew postgresComposeBuild` and

Once the application has started, you can use the application via the Swagger UI:

* `Order Service` - `http://localhost:8081/swagger-ui.html`
* `Customer Service` - `http://localhost:8082/swagger-ui.html`
* `Order Service` - `http://localhost:8081/swagger-ui/index.html`
* `Customer Service` - `http://localhost:8082/swagger-ui/index.html`
* `API Gateway` - `http://localhost:8083/swagger-ui.html`

You can also use `curl` to interact with the services.
Expand Down
4 changes: 4 additions & 0 deletions api-gateway-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ dependencies {
testCompile "junit:junit:4.12"
testCompile "org.springframework.boot:spring-boot-starter-test"
}

bootJar {
requiresUnpack '**/eventuate-util-swagger-ui-*.jar'
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@RestController
public class SwaggerController {
@GetMapping("/swagger-ui.html")
@GetMapping("/swagger-ui/index.html")
public Resource getFile() {
return new ClassPathResource("META-INF/swagger-ui/index.html");
}
Expand Down
10 changes: 9 additions & 1 deletion api-gateway-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


spring.webflux.static-path-pattern: /**

spring.application.name: api-gateway
Expand Down Expand Up @@ -27,9 +29,15 @@ resilience4j.circuitbreaker:
waitDurationInOpenState: 10000
failureRateThreshold: 60

management:
endpoints:
web:
exposure:
include: "*"

spring:
resources:
static-locations: classpath:/static, classpath:META-INF/swagger-ui
static-locations: classpath:/static, classpath:META-INF
Copy link
Contributor

@cer cer May 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the UI in the META-INF folder?
AFAIK That's not a good use for the this folder. See specification
Exposing the entire classpath:META-INF folder seems like an especially bad idea since unlike META-INF/swagger-ui it contains non-UI files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will investigate and fix

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From quickly reading https://www.webjars.org/documentation#servlet3 it looks like having static resources in a subfolder of META-INF is a common practice.

I think something like static-locations: classpath:/static, classpath:META-INF/swagger-ui is ok but static-locations: classpath:/static, classpath:META-INF is not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry, but I don't understand: "static-locations: classpath:/static, ... is ok", but then "static-locations: classpath:/static, ..." is not ok. so "classpath:/static" is ok and is not ok in the same time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, got it, it is quote from config. Sorry.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exposing the entire classpath:META-INF folder is not a good idea.

cloud:
gateway:
routes:
Expand Down
2 changes: 2 additions & 0 deletions customer-service/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.sleuth.enabled=true
spring.sleuth.sampler.probability=1
spring.zipkin.base.url=http://${DOCKER_HOST_IP:localhost}:9411/

management.endpoints.web.exposure.include=*
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.eventuate.examples.tram.sagas.ordersandcustomers.orders.api.web.CreateOrderResponse;
import io.eventuate.examples.tram.sagas.ordersandcustomers.orders.api.web.GetOrderResponse;
import io.eventuate.util.test.async.Eventually;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -21,6 +22,11 @@
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

Expand Down Expand Up @@ -94,7 +100,7 @@ public void shouldSupportOrderHistory() {
new CreateOrderRequest(createCustomerResponse.getCustomerId(), new Money("100.00")),
CreateOrderResponse.class);

Eventually.eventually(() -> {
Eventually.eventually(60, 500, TimeUnit.MILLISECONDS, () -> {
ResponseEntity<GetCustomerHistoryResponse> customerResponseEntity =
restTemplate.getForEntity(baseUrl("customers", Long.toString(createCustomerResponse.getCustomerId()), "orderhistory"),
GetCustomerHistoryResponse.class);
Expand All @@ -112,8 +118,25 @@ public void shouldSupportOrderHistory() {
});
}

@Test
public void testSwaggerUiUrls() throws IOException {
testSwaggerUiUrl(8081);
testSwaggerUiUrl(8082);
testSwaggerUiUrl(8083);
}

private void testSwaggerUiUrl(int port) throws IOException {
assertUrlStatusIsOk(String.format("http://%s:%s/swagger-ui/index.html", hostName, port));
}

private void assertUrlStatusIsOk(String url) throws IOException {
HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();

Assert.assertEquals(200, connection.getResponseCode());
}

private void assertOrderState(Long id, OrderState expectedState, RejectionReason expectedRejectionReason) {
Eventually.eventually(() -> {
Eventually.eventually(60, 500, TimeUnit.MILLISECONDS, () -> {
ResponseEntity<GetOrderResponse> getOrderResponseEntity = restTemplate.getForEntity(baseUrl("orders/" + id), GetOrderResponse.class);
assertEquals(HttpStatus.OK, getOrderResponseEntity.getStatusCode());
GetOrderResponse order = getOrderResponseEntity.getBody();
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ springBootVersion=2.2.6.RELEASE
springCloudSleuthVersion=2.2.2.RELEASE
springCloudGatewayVersion=2.2.2.RELEASE
springCloudContractDependenciesVersion=2.2.0.RELEASE
eventuateUtilVersion=0.10.0.RELEASE
eventuateUtilVersion=0.11.0-SNAPSHOT

eventuateTramSagasImageVersion=0.18.0.RELEASE

Expand Down
2 changes: 2 additions & 0 deletions order-service/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.sleuth.enabled=true
spring.sleuth.sampler.probability=1
spring.zipkin.base.url=http://${DOCKER_HOST_IP:localhost}:9411/

management.endpoints.web.exposure.include=*