From edf68e39a02bbc7d1498e75deb61b6be65f5af47 Mon Sep 17 00:00:00 2001 From: Tim Head Date: Sat, 25 May 2019 11:34:54 +0200 Subject: [PATCH] Make the URL used to generate launch badges configurable --- binderhub/app.py | 14 +++++++++++++- binderhub/main.py | 1 + binderhub/static/js/index.js | 8 +++++++- binderhub/static/js/src/badge.js | 9 ++++++++- binderhub/templates/index.html | 1 + 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/binderhub/app.py b/binderhub/app.py index 4be076eaf..2ac5339ab 100644 --- a/binderhub/app.py +++ b/binderhub/app.py @@ -123,7 +123,6 @@ def _log_level(self): '/', help="The base URL of the entire application", config=True) - @validate('base_url') def _valid_base_url(self, proposal): if not proposal.value.startswith('/'): @@ -132,6 +131,18 @@ def _valid_base_url(self, proposal): proposal.value = proposal.value + '/' return proposal.value + badge_base_url = Unicode( + '', + help="Base URL to use when generating launch badges", + config=True + ) + @validate('badge_base_url') + def _valid_badge_base_url(self, proposal): + # add a trailing slash only when a value is set + if proposal.value and not proposal.value.endswith('/'): + proposal.value = proposal.value + '/' + return proposal.value + auth_enabled = Bool( False, help="""If JupyterHub authentication enabled, @@ -506,6 +517,7 @@ def initialize(self, *args, **kwargs): 'build_memory_limit': self.build_memory_limit, 'build_docker_host': self.build_docker_host, 'base_url': self.base_url, + 'badge_base_url': self.badge_base_url, "static_path": os.path.join(HERE, "static"), 'static_url_prefix': url_path_join(self.base_url, 'static/'), 'template_variables': self.template_variables, diff --git a/binderhub/main.py b/binderhub/main.py index 80c7bc1dc..97412f6ec 100644 --- a/binderhub/main.py +++ b/binderhub/main.py @@ -15,6 +15,7 @@ class MainHandler(BaseHandler): def get(self): self.render_template( "index.html", + badge_base_url=self.settings['badge_base_url'], base_url=self.settings['base_url'], submit=False, google_analytics_code=self.settings['google_analytics_code'], diff --git a/binderhub/static/js/index.js b/binderhub/static/js/index.js index f38b377a6..1a1366947 100644 --- a/binderhub/static/js/index.js +++ b/binderhub/static/js/index.js @@ -29,6 +29,7 @@ import '../index.css'; import {fit} from './vendor/xterm/addons/fit'; var BASE_URL = $('#base-url').data().url; +var BADGE_BASE_URL = $('#badge-base-url').data().url; function update_favicon(path) { var link = document.querySelector("link[rel*='icon']") || document.createElement('link'); @@ -44,7 +45,12 @@ function v2url(providerPrefix, repository, ref, path, pathType) { // no repo, no url return null; } - var url = window.location.origin + BASE_URL + 'v2/' + providerPrefix + '/' + repository + '/' + ref; + if (BADGE_BASE_URL) { + var url = BADGE_BASE_URL + 'v2/' + providerPrefix + '/' + repository + '/' + ref; + } + else { + var url = window.location.origin + BASE_URL + 'v2/' + providerPrefix + '/' + repository + '/' + ref; + } if (path && path.length > 0) { // encode the path, it will be decoded in loadingMain url = url + '?' + pathType + 'path=' + encodeURIComponent(path); diff --git a/binderhub/static/js/src/badge.js b/binderhub/static/js/src/badge.js index c09ae3c46..52a5dbd38 100644 --- a/binderhub/static/js/src/badge.js +++ b/binderhub/static/js/src/badge.js @@ -1,5 +1,12 @@ var BASE_URL = $("#base-url").data().url; -var BADGE_URL = window.location.origin + BASE_URL + "badge_logo.svg"; +var BADGE_BASE_URL = $('#badge-base-url').data().url; + +if (BADGE_BASE_URL) { + var BADGE_URL = BADGE_BASE_URL + "badge_logo.svg"; +} +else { + var BADGE_URL = window.location.origin + BASE_URL + "badge_logo.svg"; +} export function markdownBadge(url) { // return markdown badge snippet diff --git a/binderhub/templates/index.html b/binderhub/templates/index.html index 8ab02e811..2ad068a4c 100644 --- a/binderhub/templates/index.html +++ b/binderhub/templates/index.html @@ -2,6 +2,7 @@ {% block head %} + {{ super() }} {% endblock head %}