Skip to content

Commit

Permalink
perf($Swagger): support service filter
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Feb 7, 2021
1 parent c0dc896 commit f07e0de
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.jmsoftware.maf.apigateway.universal.configuration;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotBlank;
import java.util.Set;

/**
* <h1>SwaggerConfiguration</h1>
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 2/7/2021 3:28 PM
**/
@Data
@Validated
@Component
@ConfigurationProperties(prefix = "maf.configuration.swagger")
public class SwaggerConfiguration {
/**
* Ignored service id set
*/
private Set<@NotBlank String> ignoredServiceIdSet;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.jmsoftware.maf.apigateway.universal.configuration;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Sets;
import com.jmsoftware.maf.reactivespringcloudstarter.configuration.MafProjectProperty;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -18,8 +20,10 @@
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
* <h1>SwaggerResourceProvider</h1>
Expand All @@ -43,6 +47,7 @@ public class SwaggerResourceProvider implements SwaggerResourcesProvider {
private static final String LINE_SEPARATOR = System.lineSeparator();
private final MafProjectProperty mafProjectProperty;
private final RouteLocator routeLocator;
private final SwaggerConfiguration swaggerConfiguration;

/**
* Generate Swagger resource.
Expand All @@ -56,14 +61,16 @@ public List<SwaggerResource> get() {
val swaggerResourceList = new LinkedList<SwaggerResource>();
routeLocator.getRoutes().subscribe(route -> {
val serviceName = route.getUri().toString().substring(5).toLowerCase();
log.warn("{} found dynamic route. Service name: {}, route: {}", this.getClass().getSimpleName(),
serviceName, route);
val swaggerResource = new SwaggerResource();
swaggerResource.setName(serviceName.toUpperCase());
swaggerResource.setLocation(String.format("%s%s", serviceName, SWAGGER_API_URI));
swaggerResource.setSwaggerVersion("2.0");
log.warn("Exposed Swagger Resource: {}", swaggerResource.toString());
swaggerResourceList.add(swaggerResource);
if (!CollUtil.contains(swaggerConfiguration.getIgnoredServiceIdSet(), serviceName)) {
log.warn("{} found dynamic route. Service name: {}, route: {}", this.getClass().getSimpleName(),
serviceName, route);
val swaggerResource = new SwaggerResource();
swaggerResource.setName(serviceName.toUpperCase());
swaggerResource.setLocation(String.format("%s%s", serviceName, SWAGGER_API_URI));
swaggerResource.setSwaggerVersion("2.0");
log.warn("Exposed Swagger Resource: {}", swaggerResource.toString());
swaggerResourceList.add(swaggerResource);
}
});
return swaggerResourceList;
}
Expand Down
8 changes: 5 additions & 3 deletions api-gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: Asia/Hong_Kong
sleuth:
sampler:
probability: 1.0
cloud:
consul:
discovery:
Expand Down Expand Up @@ -99,3 +96,8 @@ maf:
- "/*/v2/api-docs/**"
- "/webjars/**"
- "/doc.html"
swagger:
ignored-service-id-set:
- "consul"
- "api-gateway"
- "spring-boot-admin"

0 comments on commit f07e0de

Please sign in to comment.