Parcourir la source

Add of prefix url when running app

Jérôme BUISINE il y a 4 ans
Parent
commit
5ff66b6af3
7 fichiers modifiés avec 52 ajouts et 19 suppressions
  1. 2 3
      Dockerfile
  2. 13 0
      README.md
  3. 3 4
      docker-compose.yml
  4. 7 2
      launcher/settings.py
  5. 5 1
      links/templates/base.html
  6. 20 8
      links/views.py
  7. 2 1
      static/js/files.js

+ 2 - 3
Dockerfile

@@ -19,8 +19,7 @@ RUN python manage.py makemigrations
 RUN python manage.py migrate
 
 # only comment in case it will be necessary
-# RUN echo $WEBEXPE_PREFIX_URL
-# RUN WEBEXPE_PREFIX_URL=$WEBEXPE_PREFIX_URL
-# RUN WEB_API_PREFIX_URL=$WEB_API_PREFIX_URL
+RUN echo $WEB_PREFIX_URL
+RUN WEB_PREFIX_URL=$WEB_PREFIX_URL
 
 CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

+ 13 - 0
README.md

@@ -68,6 +68,19 @@ You also have `stop`, `remove`, `clean` commands:
 - `remove`: stop and remove container instance if exists
 - `clean`: remove docker image if exists
 
+### 3. Notes
+
+Configure your own URL prefix using `WEB_PREFIX_URL`:
+
+```
+WEB_PREFIX_URL=experiments python manage.py runserver
+```
+
+or using docker:
+
+```
+WEB_PREFIX_URL=experiments make deploy
+```
 
 ## Licence
 

+ 3 - 4
docker-compose.yml

@@ -9,9 +9,8 @@ services:
         volumes:
             - "./media:/usr/src/app/media" # get access to media files
         ports:
-           - "8000:8000"
+           - "${PORT:-8000}:8000"
         
         # only comment in case it will be necessary
-        # environment:
-        #    WEBEXPE_PREFIX_URL: "${WEBEXPE_PREFIX_URL:-}"
-        #    WEB_API_PREFIX_URL: "${WEB_API_PREFIX_URL:-api}"
+        environment:
+           WEB_PREFIX_URL: "${WEB_PREFIX_URL:-}"

+ 7 - 2
launcher/settings.py

@@ -25,7 +25,7 @@ SECRET_KEY = 'a(wm02y7+q^voeqj9i8w&9*ryvtn0gg3bo$-k=()oz!8+5_okg'
 # SECURITY WARNING: don't run with debug turned on in production!
 DEBUG = True
 
-ALLOWED_HOSTS = []
+ALLOWED_HOSTS = ["localhost", "127.0.0.1"]
 
 
 # Application definition
@@ -125,4 +125,9 @@ STATICFILES_DIRS = (
 )
 
 MEDIA_ROOT = "media/"
-MEDIA_URL = "media/"
+MEDIA_URL = "media/"
+
+# env variables
+WEB_PREFIX_URL_KEY       = 'WEB_PREFIX_URL'
+WEB_PREFIX_URL           = os.environ.get(WEB_PREFIX_URL_KEY) \
+                            if os.environ.get(WEB_PREFIX_URL_KEY) is not None else ''

+ 5 - 1
links/templates/base.html

@@ -25,7 +25,11 @@
     </div>
 
     <!-- Global scripts used -->
-    <script type="text/javascript"> 
+    <script type="text/javascript">
+        const BASE     = "{{BASE}}"
+        var baseUrl    = location.protocol + '//' + window.location.host + '/'
+
+        if (BASE !== '') baseUrl += BASE + '/'
     </script>
 
     <!-- Custom Javascript file for experiments template is placed here -->

+ 20 - 8
links/views.py

@@ -12,6 +12,17 @@ import json
 from . import config  as cfg
 
 
+def get_base_data(expe_name=None):
+    '''
+    Used to store default data to send for each view
+    '''
+    data = {}
+
+    data['BASE'] = settings.WEB_PREFIX_URL
+
+    return data
+
+
 def list_files(request):
 
     # get param 
@@ -19,11 +30,13 @@ def list_files(request):
 
     experiment_path = cfg.expe_data_folder
 
-    files = sorted(os.listdir(experiment_path))
+    files = []
+
+    if os.path.exists(experiment_path):
+        files = sorted(os.listdir(experiment_path))
 
-    data = {
-        'folder': files
-    }
+    data = get_base_data()
+    data['folder'] = files
 
     return render(request, 'links/files.html', data)
 
@@ -50,10 +63,9 @@ def user_links(request):
     for line in lines:
         data = line.split(';')
         links[data[0]] = data[1:]
-            
-    data = {
-        'links': json.dumps(links)
-    }
+    
+    data = get_base_data()
+    data['links'] = json.dumps(links)
     
     return render(request, 'links/links.html', data)
 

+ 2 - 1
static/js/files.js

@@ -1,6 +1,6 @@
 const toggleVisible = ele => ele.style.display = ele.style.display === 'none' ? 'block' : 'none'
 const toggleClass = (ele, class1, class2) => ele.className = ele.className === class1 ? class2 : class1
-const baseUrl    = location.protocol + '//' + window.location.host + '/'
+
 window.addEventListener('DOMContentLoaded', () => {
     // Display list of files from day folder
     // need to parse as `Array`
@@ -12,6 +12,7 @@ window.addEventListener('DOMContentLoaded', () => {
             // get list element
             let filePath = currentElem.getAttribute('data-redirect-path')
 
+            // use of base url obtained from Django using `{{BASE}}`
             window.location = baseUrl + 'links?filename=' + filePath
         })
     })