Browse Source

now possible to pass a URL_PREFIX env var to serve the app in a folder

Jocelyn Delande 10 years ago
parent
commit
53af8b8252
3 changed files with 15 additions and 4 deletions
  1. 3 0
      README.md
  2. 11 3
      backend.py
  3. 1 1
      views/thanks.tpl

+ 3 - 0
README.md

@@ -33,6 +33,9 @@ You can specify listening port and address by setting `BIND_PORT` and
 
 Default is to listen on `127.0.0.0`, port `8080`.
 
+You can also pass a `URL_PREFIX='/some_folder/'` if you don't want the app to be
+served at the root of the domain.
+
 Create the DataBase
 ===================
 

+ 11 - 3
backend.py

@@ -13,6 +13,7 @@ from os.path import join, dirname
 
 from bottle import route, run, static_file, request, template, FormsDict, redirect, response, Bottle
 
+URL_PREFIX = os.environ.get('URL_PREFIX', '')
 
 ORIENTATIONS = (
     ('N', 'Nord'),
@@ -197,7 +198,7 @@ def submit_wifi_form():
         # Rebuild GeoJSON
         build_geojson()
 
-        return redirect(urlparse.urljoin(request.path,'thanks'))
+        return redirect(urlparse.urljoin(request.path,join(URL_PREFIX,'thanks')))
 
 @app.route('/thanks')
 def wifi_form_thanks():
@@ -358,6 +359,7 @@ def build_geojson():
 DEBUG = bool(os.environ.get('DEBUG', False))
 LISTEN_ADDR= os.environ.get('BIND_ADDR', 'localhost')
 LISTEN_PORT= int(os.environ.get('BIND_PORT', 8080))
+URL_PREFIX = os.environ.get('URL_PREFIX', '').strip('/')
 
 if __name__ == '__main__':
     if len(sys.argv) > 1:
@@ -366,5 +368,11 @@ if __name__ == '__main__':
         if sys.argv[1] == 'buildgeojson':
             build_geojson()
     else:
-        run(app, host=LISTEN_ADDR, port=LISTEN_PORT, reloader=DEBUG)
-        DB.close()
+         if URL_PREFIX:
+              print('Using url prefix "{}"'.format(URL_PREFIX))
+              root_app = Bottle()
+              root_app.mount('/{}/'.format(URL_PREFIX), app)
+              run(root_app, host=LISTEN_ADDR, port=LISTEN_PORT, reloader=DEBUG)
+         else:
+              run(app, host=LISTEN_ADDR, port=LISTEN_PORT, reloader=DEBUG)
+         DB.close()

+ 1 - 1
views/thanks.tpl

@@ -14,7 +14,7 @@ l'association.
 </p>
 
 <p>
-Vous pouvez consulter la <a href="../map">carte publique avec tous les autres contributions</a>.
+Vous pouvez consulter la <a href="./map">carte publique avec tous les autres contributions</a>.
 </p>