Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Make the URL used to generate launch badges configurable #859

Merged
merged 1 commit into from
Jun 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion binderhub/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('/'):
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions binderhub/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MainHandler(BaseHandler):
def get(self):
self.render_template(
"index.html",
badge_base_url=self.settings['badge_base_url'],
Copy link
Collaborator

@bitnik bitnik May 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
badge_base_url=self.settings['badge_base_url'],
badge_base_url=self.settings['badge_base_url'] or
f"{self.request.protocol}://{self.request.host}{self.settings['base_url']}",

maybe this is better. so you can remove if elses in js code and it becomes cleaner.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure which one I prefer. I agree the JS has a few extra if statements, but I think I prefer it that way because it leaves the choice to the JS code. I dunno, it is just a feeling :-/

base_url=self.settings['base_url'],
submit=False,
google_analytics_code=self.settings['google_analytics_code'],
Expand Down
8 changes: 7 additions & 1 deletion binderhub/static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion binderhub/static/js/src/badge.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions binderhub/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

{% block head %}
<meta id="base-url" data-url="{{base_url}}">
<meta id="badge-base-url" data-url="{{badge_base_url}}">
<script src="{{static_url("dist/bundle.js")}}"></script>
{{ super() }}
{% endblock head %}
Expand Down