You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2022/12/27 03:39:13 UTC
[servicecomb-java-chassis] branch master updated: [SCB-2737] export swagger contents to temporary files (#3513)
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new 1ac06ab7c [SCB-2737] export swagger contents to temporary files (#3513)
1ac06ab7c is described below
commit 1ac06ab7cf84c9c4dd6c18e6d4a9a2c3a444d856
Author: yanghao <73...@users.noreply.github.com>
AuthorDate: Tue Dec 27 11:39:07 2022 +0800
[SCB-2737] export swagger contents to temporary files (#3513)
---
.../provider/producer/ProducerBootListener.java | 36 ++++++++++++++++++++++
.../registry/definition/DefinitionConst.java | 4 +++
2 files changed, 40 insertions(+)
diff --git a/core/src/main/java/org/apache/servicecomb/core/provider/producer/ProducerBootListener.java b/core/src/main/java/org/apache/servicecomb/core/provider/producer/ProducerBootListener.java
index 5ac82a6d3..b37f11cc3 100644
--- a/core/src/main/java/org/apache/servicecomb/core/provider/producer/ProducerBootListener.java
+++ b/core/src/main/java/org/apache/servicecomb/core/provider/producer/ProducerBootListener.java
@@ -18,10 +18,14 @@
package org.apache.servicecomb.core.provider.producer;
import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
+import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.core.BootListener;
import org.apache.servicecomb.core.definition.MicroserviceMeta;
@@ -45,8 +49,17 @@ import io.swagger.models.Swagger;
public class ProducerBootListener implements BootListener {
private static final Logger LOGGER = LoggerFactory.getLogger(ProducerBootListener.class);
+ private static final String PATTERN = File.separator + "microservices"
+ + File.separator + "%s" + File.separator + "%s.yaml";
+
+ private static final String TMP_DIR = System.getProperty("java.io.tmpdir");
+
@Override
public void onAfterTransport(BootEvent event) {
+ boolean exportToFile = DynamicPropertyFactory.getInstance()
+ .getBooleanProperty(DefinitionConst.SWAGGER_EXPORT_ENABLED, true).get();
+ String filePath = DynamicPropertyFactory.getInstance()
+ .getStringProperty(DefinitionConst.SWAGGER_DIRECTORY, TMP_DIR).get() + PATTERN;
// register schema to microservice;
Microservice microservice = RegistrationManager.INSTANCE.getMicroservice();
@@ -63,6 +76,9 @@ public class ProducerBootListener implements BootListener {
Swagger swagger = schemaMeta.getSwagger();
swagger.addScheme(Scheme.forValue(swaggerSchema));
String content = SwaggerUtils.swaggerToString(swagger);
+ if (exportToFile) {
+ exportToFile(String.format(filePath, microservice.getServiceName(), schemaMeta.getSchemaId()), content);
+ }
LOGGER.info("generate swagger for {}/{}/{}, swagger: {}",
microserviceMeta.getAppId(),
microserviceMeta.getMicroserviceName(),
@@ -123,4 +139,24 @@ public class ProducerBootListener implements BootListener {
operationMeta.getExecutor().getClass().getName());
}
}
+
+ private void exportToFile(String fileName, String content) {
+ File file = new File(fileName);
+ if (!file.getParentFile().exists()) {
+ if (!file.getParentFile().mkdirs()) {
+ LOGGER.error("create file directory failed");
+ return;
+ }
+ }
+ if (file.exists()) {
+ file.delete();
+ }
+ try {
+ file.createNewFile();
+ FileUtils.writeStringToFile(file, content, StandardCharsets.UTF_8, false);
+ file.setReadOnly();
+ } catch (IOException e) {
+ LOGGER.error("export swagger content to file failed, message: {}", e.getMessage());
+ }
+ }
}
diff --git a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/definition/DefinitionConst.java b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/definition/DefinitionConst.java
index deedacd1b..898bc3a86 100644
--- a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/definition/DefinitionConst.java
+++ b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/definition/DefinitionConst.java
@@ -40,4 +40,8 @@ public interface DefinitionConst {
String REGISTRY_APP_ID = "default";
String REGISTRY_SERVICE_NAME = "SERVICECENTER";
+
+ String SWAGGER_EXPORT_ENABLED = "servicecomb.swagger.export.enabled";
+
+ String SWAGGER_DIRECTORY = "servicecomb.swagger.export.directory";
}