Commit 6c8d9299 authored by Kevin Di Lallo's avatar Kevin Di Lallo
Browse files

sandbox-ctrl entrypoint script improvements + root URL prefix addition for swagger UI yaml files

parent 6dabb003
Loading
Loading
Loading
Loading
+20 −25
Original line number Diff line number Diff line
#!/bin/bash
set -e

echo "MEEP_HOST_URL: ${MEEP_HOST_URL}"
echo "MEEP_SANDBOX_NAME: ${MEEP_SANDBOX_NAME}"
echo "USER_SWAGGER: ${USER_SWAGGER}"
echo "USER_SWAGGER_SANDBOX: ${USER_SWAGGER_SANDBOX}"

# Prepend sandbox name to REST API yaml files
# Update API yaml basepaths to enable "Try-it-out" feature
# OAS2: Set relative path to sandbox name + endpoint path (origin will be derived from browser URL)
# OAS3: Set full path to provided Host URL + sandbox name + endpoint path
setBasepath() {
    # OAS2
    echo "Prepending [${MEEP_SANDBOX_NAME}] to basepath in: $1"
    sed -i 's,basePath: \"/\?,basePath: \"/'${MEEP_SANDBOX_NAME}'/,' $1;

    # OAS3
    hostName=$(echo "${MEEP_HOST_URL}" | sed -E 's/^\s*.*:\/\///g')
    newHostName=${hostName}/${MEEP_SANDBOX_NAME}
    echo "Replacing [localhost] with ${newHostName} to url in: $1"
    sed -i "s,localhost,${newHostName},g" $1;
}

# Set baspath for AdvantEDGE Swagger API files
for file in /swagger/*-api.yaml; do
    echo "Prepending [${MEEP_SANDBOX_NAME}] to basepath in: $file"
    sed -i 's,basePath: \"/\?,basePath: \"/'${MEEP_SANDBOX_NAME}'/,' $file;
    echo "Replacing localhost with ${MEEP_SANDBOX_NAME} to url in: $file"
    #sed -i 's/{apiRoot}/'${MEEP_SANDBOX_NAME}'/g' $file;
    newValue=${MEEP_HOST_URL}/${MEEP_SANDBOX_NAME}
    httpPrefixToRemove='http://'
    httpsPrefixToRemove='https://'
    echo $newValue
    newValueSuffix="${newValue/$httpsPrefixToRemove/}"
    newValueSuffix="${newValueSuffix/$httpPrefixToRemove/}"
    echo $newValueSuffix
    sed -i "s@localhost@$newValueSuffix@g" $file;
    setBasepath $file
done

# Copy user-swagger & adapt basepath to sandbox
# Set baspath for User-provided Swagger API files
if [[ ! -z "${USER_SWAGGER}" ]]; then
    cp -r ${USER_SWAGGER} ${USER_SWAGGER_SANDBOX}
    shopt -s nullglob
    for file in ${USER_SWAGGER_SANDBOX}/*-api.yaml; do
        echo "Prepending [${MEEP_SANDBOX_NAME}] to basepath in: $file"
        sed -i 's,basePath: \"/\?,basePath: \"/'${MEEP_SANDBOX_NAME}'/,' $file;
        echo "Replacing localhost with ${MEEP_SANDBOX_NAME} to url in: $file"
        newValue=${MEEP_HOST_URL}/${MEEP_SANDBOX_NAME}
        httpPrefixToRemove='http://'
        httpsPrefixToRemove='https://'
        echo $newValue
        newValueSuffix="${newValue/$httpsPrefixToRemove/}"
        newValueSuffix="${newValueSuffix/$httpPrefixToRemove/}"
        echo $newValueSuffix
        sed -i "s@localhost@$newValueSuffix@g" $file;
        setBasepath $file
    done
fi

+2 −2
Original line number Diff line number Diff line
@@ -327,13 +327,13 @@ func buildSwaggerUi(targetName string, repo string, cobraCmd *cobra.Command) {
			}

			//update urls for swagger-ui index file
			urls = urls + `{"name": "` + title + `", "url": "` + name + `-api.yaml"},`
			urls = urls + `{"name": "` + title + `", "url": rootUrl + "` + name + `-api.yaml"},`
		}
	}

	//update swagger-ui index file
	urls = urls + " ],"
	sedString := "s/" + urlStringToReplace + "/urls: " + urls + "/g"
	sedString := "s@" + urlStringToReplace + "@urls: " + urls + "@g"
	cmd = exec.Command("sed", "-i", sedString, binDir+"/index.html")
	_, err = utils.ExecuteCmd(cmd, cobraCmd)
	if err != nil {
+1 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@
    <script src="./swagger-ui-standalone-preset.js"> </script>
    <script>
    window.onload = function() {
      const rootUrl = location.protocol + '//' + location.host + location.pathname;
      // Begin Swagger UI call region
      const ui = SwaggerUIBundle({
        url: "https://petstore.swagger.io/v2/swagger.json",