Commit 719d9b71 authored by BAIRE Anthony's avatar BAIRE Anthony
Browse files

add the get_base_url() helper to remove duplicated code

This also fixes the resolution of the port when it is provided in the
X-Forwarded-Host header.
parent 8c1d3e7c
......@@ -6,7 +6,7 @@ import config.env
from django.http import HttpResponse, JsonResponse, FileResponse
from django.views.decorators.csrf import csrf_exempt
from main.models import Job, AllgoUser, Webapp, JobQueue
from main.helpers import upload_data
from main.helpers import upload_data, get_base_url
log = logging.getLogger('allgo')
......@@ -36,10 +36,8 @@ def sha1file(filepath):
def get_link(jobid, dir, filename, request):
path = ''.join(('http', ('', 's')[request.is_secure()], '://', request.META['HTTP_HOST']))
path += "/api/v1"
filepath = os.path.join(dir, filename)
return '/'.join((path, 'datastore', str(jobid), sha1file(filepath), filename))
return '/'.join((get_base_url(request), "api/v1/datastore", str(jobid), sha1file(filepath), filename))
def job_response(job, request):
......
......@@ -132,3 +132,17 @@ def is_allowed_ip_admin(ip):
def get_base_url(request):
"""Extract the base url from this django request object
typically this will be "https://allgo.inria.fr"
"""
scheme = request.META.get("HTTP_X_FORWARDED_PROTO", request.scheme)
# NOTE: django's request.get_host()/.get_port() are kind of broken because
# they do not expect the port to be provided in the Host/X-Forwarded-Host
# headers (which is quite common)
host = request.META.get("HTTP_X_FORWARDED_HOST")
if host is None:
host = request.get_host()
return "%s://%s" % (scheme, host)
......@@ -64,7 +64,7 @@ from .forms import (
WebappForm,
WebappSandboxForm,
)
from .helpers import get_ssh_data, upload_data
from .helpers import get_ssh_data, upload_data, get_base_url
from .templatetags.converters import status_icon
......@@ -598,6 +598,8 @@ class JobCreate(SuccessMessageMixin, CreateView):
versions.reverse()
kwargs['versions'] = versions
kwargs['base_url'] = get_base_url(self.request)
return super().get_context_data(**kwargs)
def get_form_kwargs(self):
......@@ -796,6 +798,9 @@ class RunnerUpdate(SuccessMessageMixin, LoginRequiredMixin, UpdateView):
kwargs['request'] = self.request
return kwargs
def get_context_data(self, **kwargs):
kwargs["base_url"] = get_base_url(self.request)
return super().get_context_data(**kwargs)
class RunnerDelete(SuccessMessageMixin, LoginRequiredMixin, DeleteView):
"""Delete a runner
......
......@@ -59,12 +59,12 @@
For more information, refer to <a href="#">documentation</a>.
</small>
<div class="input-group">
<input type="text" class="form-control" placeholder="docker run -v /var/run/docker.sock:/var/run/docker.sock --net=host allgo/runner - {{ object.token }} {{ request.META.HTTP_X_FORWARDED_PROTO }}://{{ request.get_host }}{% if request.port %}:{{ request.port }}{% endif %}" readonly>
<input type="text" class="form-control" placeholder="docker run -v /var/run/docker.sock:/var/run/docker.sock --net=host allgo/runner - {{ object.token }} {{ base_url }}" readonly>
<span class="input-group-append">
<button
class="btn btn-primary js-copy"
data-toggle="tooltip"
data-copy="docker run -v /var/run/docker.sock:/var/run/docker.sock --net=host allgo/runner - {{ object.token }} {{ request.META.HTTP_X_FORWARDED_PROTO }}://{{ request.get_host }}{% if request.port %}:{{ request.port }}{% endif %}"
data-copy="docker run -v /var/run/docker.sock:/var/run/docker.sock --net=host allgo/runner - {{ object.token }} {{ base_url }}"
data-placement="top"
title="Copy to clipboard" type="button"><i class="fas fa-clipboard"></i><span class="text-hide">Copy to clipboard</span></button>
</span>
......
......@@ -133,13 +133,13 @@ curl -H &#39;Authorization: Token token={% if user.is_authenticated %}{{ user.al
-F files[0]=@test.txt
-F files[1]=@test2.csv
-F job[file_url]=&lt;my_file_url&gt;
-F job[dataset]=&lt;my_dataset_name&gt; {{ request.scheme }}://{{ request.get_host }}{% if request.port %}:{{ request.port }}{% endif %}{% url 'api:jobs' %}</code></pre>
-F job[dataset]=&lt;my_dataset_name&gt; {{ base_url }}{% url 'api:jobs' %}</code></pre>
</div>
<div class="col">
<button
class="btn btn-primary js-copy"
data-toggle="tooltip"
data-copy="curl -H &#39;Authorization: Token token={% if user.is_authenticated %}{{ user.allgouser.token }}{% else %}&lt;your_private_token&gt;{% endif %}&#39; -X POST -F job[webapp_id]={{ webapp.id }} -F job[param]=&quot;&quot; -F job[queue]=standard -F files[0]=@test.txt -F files[1]=@test2.csv -F job[file_url]=&lt;my_file_url&gt; -F job[dataset]=&lt;my_dataset_name&gt; {{ request.scheme }}://{{ request.get_host }}{% if request.port %}:{{ request.port }}{% endif %}{% url 'api:jobs' %}"
data-copy="curl -H &#39;Authorization: Token token={% if user.is_authenticated %}{{ user.allgouser.token }}{% else %}&lt;your_private_token&gt;{% endif %}&#39; -X POST -F job[webapp_id]={{ webapp.id }} -F job[param]=&quot;&quot; -F job[queue]=standard -F files[0]=@test.txt -F files[1]=@test2.csv -F job[file_url]=&lt;my_file_url&gt; -F job[dataset]=&lt;my_dataset_name&gt; {{ base_url }}{% url 'api:jobs' %}"
data-placement="top"
title="Copy to clipboard" type="button"><i class="fas fa-clipboard"></i><span class="text-hide">Copy to clipboard</span></button>
</div>
......@@ -148,14 +148,14 @@ curl -H &#39;Authorization: Token token={% if user.is_authenticated %}{{ user.al
<p>Checkout the result:</p>
<div class="row">
<div class="col-11">
<pre><code class="lang-bash">curl -H &#39;Authorization: Token token={% if user.is_authenticated %}{{ user.allgouser.token }}{% else %}&lt;your_private_token&gt;{% endif %}&#39; -X GET {{ request.scheme }}://{{ request.get_host }}{% if request.port %}:{{ request.port }}{% endif %}{% url 'api:job' 1 %}</code></pre>
<pre><code class="lang-bash">curl -H &#39;Authorization: Token token={% if user.is_authenticated %}{{ user.allgouser.token }}{% else %}&lt;your_private_token&gt;{% endif %}&#39; -X GET {{ base_url }}{% url 'api:job' 1 %}</code></pre>
</div>
<div class="col">
<button
class="btn btn-primary js-copy"
data-toggle="tooltip"
data-copy="curl -H &#39;Authorization: Token token={% if user.is_authenticated %}{{ user.allgouser.token }}{% else %}&lt;your_private_token&gt;{% endif %}&#39; -X GET {{ request.scheme }}://{{ request.get_host }}{% if request.port %}:{{ request.port }}{% endif %}{% url 'api:job' 1 %}"
data-copy="curl -H &#39;Authorization: Token token={% if user.is_authenticated %}{{ user.allgouser.token }}{% else %}&lt;your_private_token&gt;{% endif %}&#39; -X GET {{ base_url }}{% url 'api:job' 1 %}"
data-placement="top"
title="Copy to clipboard" type="button"><i class="fas fa-clipboard"></i><span class="text-hide">Copy to clipboard</span></button>
</div>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment