Browse Source

Merge branch 'configurable-prefix'

Jocelyn Delande 10 years ago
parent
commit
54adc00f1b
4 changed files with 30 additions and 17 deletions
  1. 3 0
      README.md
  2. 2 3
      assets/map.js
  3. 24 13
      backend.py
  4. 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
 ===================
 

+ 2 - 3
assets/map.js

@@ -10,8 +10,7 @@ $( document ).ready(function() {
     // Icons
 
 	var leecherIcon = L.icon({
-		//iconUrl: '../assets/leaflet/images/marker-blue.png',
-        iconUrl: '../assets/leaflet/images/marker-icon.png',
+        iconUrl: 'assets/leaflet/images/marker-icon.png',
 		iconSize: [25, 41],
 		iconAnchor: [12, 41],
 		popupAnchor: [0, -28]
@@ -19,7 +18,7 @@ $( document ).ready(function() {
 
 
     var seederIcon = L.icon({
-        iconUrl: '../assets/leaflet/images/marker-icon-red.png',
+        iconUrl: 'assets/leaflet/images/marker-icon-red.png',
 		iconSize: [25, 41],
 		iconAnchor: [12, 41],
 		popupAnchor: [0, -28]

+ 24 - 13
backend.py

@@ -11,7 +11,9 @@ from email import utils
 from os.path import join, dirname
 
 
-from bottle import route, run, static_file, request, template, FormsDict, redirect, response
+from bottle import route, run, static_file, request, template, FormsDict, redirect, response, Bottle
+
+URL_PREFIX = os.environ.get('URL_PREFIX', '')
 
 ORIENTATIONS = (
     ('N', 'Nord'),
@@ -70,11 +72,13 @@ GEOJSON_NAME = 'public.json'
 
 ANTISPAM_FIELD = 'url'
 
-@route('/')
+app = Bottle()
+
+@app.route('/')
 def home():
-     redirect("/wifi-form")
+     redirect(urlparse.urljoin(request.path,join(URL_PREFIX, 'wifi-form')))
 
-@route('/wifi-form')
+@app.route('/wifi-form')
 def show_wifi_form():
     return template('wifi-form', errors=None, data = FormsDict(),
                     orientations=ORIENTATIONS, geojson=GEOJSON_NAME)
@@ -103,7 +107,7 @@ VALUES (:name, :contrib_type, :latitude, :longitude, :phone, :email, :access_typ
         :privacy_name, :privacy_email, :privacy_place_details, :privacy_coordinates, :privacy_comment, :date)
 """.format(TABLE_NAME), tosave)
 
-@route('/wifi-form', method='POST')
+@app.route('/wifi-form', method='POST')
 def submit_wifi_form():
     required = ('name', 'contrib-type',
                 'latitude', 'longitude')
@@ -198,18 +202,18 @@ 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')))
 
-@route('/thanks')
+@app.route('/thanks')
 def wifi_form_thanks():
     return template('thanks')
 
-@route('/assets/<filename:path>')
+@app.route('/assets/<filename:path>')
 def send_asset(filename):
     return static_file(filename, root=join(dirname(__file__), 'assets'))
 
 
-@route('/legal')
+@app.route('/legal')
 def legal():
     return template('legal')
 
@@ -218,11 +222,11 @@ def legal():
 Results Map
 """
 
-@route('/map')
+@app.route('/map')
 def public_map():
     return template('map', geojson=GEOJSON_NAME)
 
-@route('/public.json')
+@app.route('/public.json')
 def public_geojson():
     return static_file('public.json', root=join(dirname(__file__), 'json/'))
 
@@ -363,6 +367,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:
@@ -371,5 +376,11 @@ if __name__ == '__main__':
         if sys.argv[1] == 'buildgeojson':
             build_geojson()
     else:
-        run(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,5 +14,5 @@ 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>