From a8f5cdd548042edc6b45f309888321f952bc480d Mon Sep 17 00:00:00 2001
From: bclayton-usgs <bclayton@usgs.gov>
Date: Fri, 13 Mar 2020 09:10:26 -0600
Subject: [PATCH] fix context path on swagger docs

---
 Dockerfile                            |  5 ++-
 build.gradle                          |  2 +-
 gradle.properties                     |  5 ++-
 src/main/resources/swagger/index.html | 58 ++++++++++++++++-----------
 src/main/resources/swagger/index.js   | 42 +++++++++++++++++++
 5 files changed, 84 insertions(+), 28 deletions(-)
 create mode 100644 src/main/resources/swagger/index.js

diff --git a/Dockerfile b/Dockerfile
index 44d081e..ee73aad 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -26,6 +26,8 @@ FROM usgs/centos:8 as builder
 
 ENV LANG="en_US.UTF-8"
 
+ARG context_path=/nshmp/data
+
 ARG builder_workdir
 ARG libs_dir
 ARG jar_file
@@ -36,6 +38,8 @@ RUN yum install -y java-11-openjdk which git
 
 COPY . .
 
+ENV micronaut.server.context-path=${context_path}
+
 RUN mv nshmp-lib ../. \
     && mv nshm-fault-sections /app/faults \
     && ./gradlew --no-daemon assemble \
@@ -62,6 +66,5 @@ RUN yum install -y java-11-openjdk-headless
 
 EXPOSE 8080
 ENTRYPOINT /usr/bin/java -jar \
-    "-Dmicronaut.server.context-path=${CONTEXT_PATH}" \
     ${PROJECT}.jar \
     --faults-path=/app/faults
diff --git a/build.gradle b/build.gradle
index 6bc1b0d..5c829da 100644
--- a/build.gradle
+++ b/build.gradle
@@ -40,7 +40,7 @@ dependencies {
   runtimeOnly "ch.qos.logback:logback-classic:${logbackVersion}"
 
   // Swagger
-  annotationProcessor("io.micronaut.configuration:micronaut-openapi:${swaggerVersion}")
+  annotationProcessor("io.micronaut.configuration:micronaut-openapi:${mnOpenAPIVersion}")
   implementation("io.swagger.core.v3:swagger-annotations:${swaggerVersion}")
 
   // junit
diff --git a/gradle.properties b/gradle.properties
index e6363b6..72706be 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -1,5 +1,6 @@
 jacksonVersion = 2.9.0
 junitVersion = 5.5.2
 logbackVersion = 1.2.3
-mnVersion = 1.3.2
-swaggerVersion = 1.4.0
+mnVersion = 1.3.3
+mnOpenAPIVersion = 1.4.0
+swaggerVersion = 2.1.1
diff --git a/src/main/resources/swagger/index.html b/src/main/resources/swagger/index.html
index 1ecf5cf..0e6f7d4 100644
--- a/src/main/resources/swagger/index.html
+++ b/src/main/resources/swagger/index.html
@@ -1,26 +1,36 @@
-<!doctype html>
-<head>
-  <title>NSHMP WS Swagger Docs</title>
-  <meta charset="utf8"/>
-  <meta name="viewport" content="width=device-width, initial-scale=1"/>
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <title>NSHMP WS Services</title>
+    <meta charset="UTF-8" />
+    <link
+      rel="icon"
+      type="image/png"
+      href="https://unpkg.com/swagger-ui-dist/favicon-32x32.png"
+      sizes="32x32"
+    />
+    <link
+      rel="icon"
+      type="image/png"
+      href="https://unpkg.com/swagger-ui-dist/favicon-16x16.png"
+      sizes="16x16"
+    />
+    <script src="https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js"></script>
+    <script src="https://unpkg.com/swagger-ui-dist/swagger-ui-standalone-preset.js"></script>
+    <link
+      rel="stylesheet"
+      type="text/css"
+      href="https://unpkg.com/swagger-ui-dist/swagger-ui.css"
+    />
+    <link
+      rel="stylesheet"
+      type="text/css"
+      href="https://unpkg.com/swagger-ui-themes@3.0.0/themes/3.x/theme-material.css"
+    />
+  </head>
 
-  <link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@3/swagger-ui.css"/>
-</head>
-<body>
-  <div id="swagger-ui"></div>
-
-  <script src="https://unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
-  <script>
-    let pathname = window.location.pathname;
-    pathname = pathname == "/" ? "" : pathname;
-
-    SwaggerUIBundle({
-        url: `${pathname}swagger/nshmp-non-nshm-services.yml`,
-        dom_id: '#swagger-ui',
-        presets: [
-          SwaggerUIBundle.presets.apis
-        ]
-    });
-  </script>
-</body>
+  <body>
+    <div id="swagger-ui"></div>
+    <script src="index.js"></script>
+  </body>
 </html>
diff --git a/src/main/resources/swagger/index.js b/src/main/resources/swagger/index.js
new file mode 100644
index 0000000..12f6859
--- /dev/null
+++ b/src/main/resources/swagger/index.js
@@ -0,0 +1,42 @@
+window.onload = function() {
+  const extract = function(v) {
+    return decodeURIComponent(
+        v.replace(
+          /(?:(?:^|.*;\s*)contextPath\s*\=\s*([^;]*).*$)|^.*$/,
+          "$1"
+        )
+      );
+    },
+    cookie = extract(document.cookie),
+    contextPath = cookie === "" ? extract(window.location.search.substring(1)) : cookie,
+    f = contextPath === "" ? undefined : function(system) {
+      return {
+        statePlugins: {
+          spec: {
+            wrapActions: {
+              updateJsonSpec: (oriAction, system) => (...args) => {
+                let [spec] = args;
+                if (spec && spec.paths) {
+                  const newPaths = {};
+                  Object.entries(spec.paths)
+                      .forEach(([path, value]) => (newPaths[contextPath + path] = value));
+                  spec.paths = newPaths;
+                }
+                oriAction(...args);
+              }
+            }
+          }
+        }
+      };
+    },
+    ui = SwaggerUIBundle({
+      url: contextPath + "/swagger/nshmp-non-nshm-services.yml",
+      dom_id: "#swagger-ui",
+      tagsSorter: 'alpha',
+      presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset],
+      plugins: [SwaggerUIBundle.plugins.DownloadUrl, f],
+      validatorUrl: null,
+      deepLinking: true
+    });
+  window.ui = ui;
+};
-- 
GitLab