You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by ch...@apache.org on 2019/07/09 09:04:46 UTC

[incubator-openwhisk] branch master updated: Package swagger docs as part of standalone jar (#4542)

This is an automated email from the ASF dual-hosted git repository.

chetanm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new 83319b0  Package swagger docs as part of standalone jar (#4542)
83319b0 is described below

commit 83319b0ffac2579efe9785bf9dd324c3043ce5e4
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Tue Jul 9 14:34:38 2019 +0530

    Package swagger docs as part of standalone jar (#4542)
    
    Package swagger docs as part of standalone jar and make swagger ui routes configurable such that it can serve content either from file system (when running within Docker) or from classpath when running in standalone mode
---
 .../org/apache/openwhisk/core/WhiskConfig.scala    |  1 +
 .../controller/src/main/resources/application.conf |  4 +++
 .../openwhisk/core/controller/RestAPIs.scala       |  5 +++-
 core/standalone/build.gradle                       | 30 ++++++++++++++++++++++
 core/standalone/src/main/resources/standalone.conf |  4 +++
 5 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/common/scala/src/main/scala/org/apache/openwhisk/core/WhiskConfig.scala b/common/scala/src/main/scala/org/apache/openwhisk/core/WhiskConfig.scala
index 6283033..1b9baf4 100644
--- a/common/scala/src/main/scala/org/apache/openwhisk/core/WhiskConfig.scala
+++ b/common/scala/src/main/scala/org/apache/openwhisk/core/WhiskConfig.scala
@@ -263,4 +263,5 @@ object ConfigKeys {
   val featureFlags = "whisk.feature-flags"
 
   val whiskConfig = "whisk.config"
+  val swaggerUi = "whisk.swagger-ui"
 }
diff --git a/core/controller/src/main/resources/application.conf b/core/controller/src/main/resources/application.conf
index 6116789..8358ced 100644
--- a/core/controller/src/main/resources/application.conf
+++ b/core/controller/src/main/resources/application.conf
@@ -113,4 +113,8 @@ whisk{
   tracing {
     component = "Controller"
   }
+  swagger-ui {
+    file-system : true
+    dir-path : "/swagger-ui/"
+  }
 }
diff --git a/core/controller/src/main/scala/org/apache/openwhisk/core/controller/RestAPIs.scala b/core/controller/src/main/scala/org/apache/openwhisk/core/controller/RestAPIs.scala
index 522d2eb..6c1720d 100644
--- a/core/controller/src/main/scala/org/apache/openwhisk/core/controller/RestAPIs.scala
+++ b/core/controller/src/main/scala/org/apache/openwhisk/core/controller/RestAPIs.scala
@@ -48,10 +48,12 @@ import scala.util.{Failure, Success, Try}
  */
 protected[controller] class SwaggerDocs(apipath: Uri.Path, doc: String)(implicit actorSystem: ActorSystem)
     extends Directives {
+  case class SwaggerConfig(fileSystem: Boolean, dirPath: String)
 
   /** Swagger end points. */
   protected val swaggeruipath = "docs"
   protected val swaggerdocpath = "api-docs"
+  private val swaggerConfig = loadConfigOrThrow[SwaggerConfig](ConfigKeys.swaggerUi)
 
   def basepath(url: Uri.Path = apipath): String = {
     (if (url.startsWithSlash) url else Uri.Path./(url)).toString
@@ -62,7 +64,8 @@ protected[controller] class SwaggerDocs(apipath: Uri.Path, doc: String)(implicit
    */
   val swaggerRoutes: Route = {
     pathPrefix(swaggeruipath) {
-      getFromDirectory("/swagger-ui/")
+      if (swaggerConfig.fileSystem) getFromDirectory(swaggerConfig.dirPath)
+      else getFromResourceDirectory(swaggerConfig.dirPath)
     } ~ path(swaggeruipath) {
       redirect(s"$swaggeruipath/index.html?url=$apiDocsUrl", PermanentRedirect)
     } ~ pathPrefix(swaggerdocpath) {
diff --git a/core/standalone/build.gradle b/core/standalone/build.gradle
index e266088..020f3c6 100644
--- a/core/standalone/build.gradle
+++ b/core/standalone/build.gradle
@@ -30,10 +30,40 @@ repositories {
     mavenCentral()
 }
 
+task copySwagger(type: Copy) {
+    def version = "3.6.0"
+    mkdir("$buildDir/tmp/swagger")
+    def destFile = file("$buildDir/tmp/swagger/swagger-ui.tar")
+    def uiDir = file("$buildDir/tmp/swagger/swagger-ui")
+    if (!destFile.exists()) {
+        ant.get(src: "https://github.com/swagger-api/swagger-ui/archive/v${version}.tar.gz", dest: destFile)
+    }
+    from(tarTree(resources.gzip(destFile))){
+        include("swagger-ui-${version}/dist/**")
+    }
+    into(uiDir)
+    includeEmptyDirs = false
+    project.ext.swaggerUiDir = file("$buildDir/tmp/swagger/swagger-ui/swagger-ui-${version}/dist")
+}
+
+processResources.dependsOn copySwagger
+
 processResources {
     from(new File(project.rootProject.projectDir, "ansible/files/runtimes.json")) {
         into(".")
     }
+    //Implement the logic present in controller Docker file
+    from(project.swaggerUiDir) {
+        include "index.html"
+        filter {
+            it.replace("http://petstore.swagger.io/v2/swagger.json", "/api/v1/api-docs")
+        }
+        into("swagger-ui")
+    }
+    from(project.swaggerUiDir) {
+        exclude "index.html"
+        into("swagger-ui")
+    }
 }
 
 task copyBootJarToBin(type:Copy){
diff --git a/core/standalone/src/main/resources/standalone.conf b/core/standalone/src/main/resources/standalone.conf
index e3a89ad..c735677 100644
--- a/core/standalone/src/main/resources/standalone.conf
+++ b/core/standalone/src/main/resources/standalone.conf
@@ -68,4 +68,8 @@ whisk {
       pull-standard-images: true
     }
   }
+  swagger-ui {
+    file-system : false
+    dir-path : "BOOT-INF/classes/swagger-ui"
+  }
 }