Skip to content

Commit

Permalink
Extract inline JS in Swagger UI to initializer script
Browse files Browse the repository at this point in the history
  • Loading branch information
nadove-ucsc committed Nov 21, 2024
1 parent bff2348 commit 1ee7b6d
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 82 deletions.
16 changes: 8 additions & 8 deletions src/azul/chalice.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,12 +531,11 @@ def swagger_redirect(self) -> Response:
body='',
headers={'Location': str(self.self_url.set(path='static/index.html'))})

def swagger_ui(self) -> Response:
swagger_ui_template = self.load_static_resource('swagger', 'index.html.template.mustache')
def _expand_swagger_template(self, template: str) -> str:
base_url = self.base_url
redirect_url = furl(base_url).add(path='oauth2_redirect')
deployment_url = furl(base_url).add(path='openapi')
swagger_ui_html = chevron.render(swagger_ui_template, {
return chevron.render(template, {
'DEPLOYMENT_PATH': json.dumps(str(deployment_url.path)),
'OAUTH2_CLIENT_ID': json.dumps(config.google_oauth2_client_id),
'OAUTH2_REDIRECT_URL': json.dumps(str(redirect_url)),
Expand All @@ -545,19 +544,20 @@ def swagger_ui(self) -> Response:
for path, method in self.non_interactive_routes
])
})
return Response(status_code=200,
headers={'Content-Type': 'text/html'},
body=swagger_ui_html)

def swagger_resource(self, file) -> Response:
if os.sep in file:
raise BadRequestError(file)
else:
templated_file = 'swagger-initializer.js'
stored_file = file + '.template.mustache' if file == templated_file else file
try:
body = self.load_static_resource('swagger', file)
body = self.load_static_resource('swagger', stored_file)
except FileNotFoundError:
raise NotFoundError(file)
else:
if file == templated_file:
body = self._expand_swagger_template(body)
path = pathlib.Path(file)
content_type = mimetypes.types_map[path.suffix]
return Response(status_code=200,
Expand Down Expand Up @@ -713,7 +713,7 @@ def swagger_redirect():
}
)
def swagger_ui():
return self.swagger_ui()
return self.swagger_resource('index.html')

@self.route(
'/openapi',
Expand Down
19 changes: 19 additions & 0 deletions swagger/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
<link rel="stylesheet" type="text/css" href="index.css" />
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
</head>

<body>
<div id="swagger-ui"></div>
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
<script src="./swagger-initializer.js" charset="UTF-8"> </script>
</body>
</html>
74 changes: 0 additions & 74 deletions swagger/index.html.template.mustache

This file was deleted.

20 changes: 20 additions & 0 deletions swagger/swagger-initializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
window.onload = function() {
//<editor-fold desc="Changeable Configuration Block">

// the following lines will be replaced by docker/configurator, when it runs in a docker-container
window.ui = SwaggerUIBundle({
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});

//</editor-fold>
};
43 changes: 43 additions & 0 deletions swagger/swagger-initializer.js.template.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
window.onload = function() {
//<editor-fold desc="Changeable Configuration Block">
// Adapted from https://github.com/swagger-api/swagger-ui/issues/3725#issuecomment-334899276
const DisableTryItOutPlugin = function() {
return {
statePlugins: {
spec: {
wrapSelectors: {
allowTryItOutFor: (oriSelector, system) => (state, ...args) => {
return oriSelector(state, ...args) && ({{{NON_INTERACTIVE_METHODS}}}.indexOf(args.join('/')) == -1);
}
}
}
}
}
}

window.ui = SwaggerUIBundle({
url: {{{DEPLOYMENT_PATH}}},
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl,
DisableTryItOutPlugin
],
oauth2RedirectUrl: {{{OAUTH2_REDIRECT_URL}}},
layout: "StandaloneLayout"
});

const client_id = {{{OAUTH2_CLIENT_ID}}};
if (client_id !== null) {
window.ui.initOAuth({
clientId: client_id
})
}

//</editor-fold>
};

0 comments on commit 1ee7b6d

Please sign in to comment.