diff piecrust/admin/web.py @ 935:7ecb946bfafd

admin: Lots of fixes for running the admin panel in a WSGI server. - Use new source APIs in the dashboard to open WIP files. - Fixed broken/outdated code in some views. - Fixed cases when Flask is not running at the root URL by using the `SCRIPT_NAME` variable somewhat more properly.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 04 Oct 2017 09:15:16 -0700
parents ca357249a321
children e1cadbfddb48
line wrap: on
line diff
--- a/piecrust/admin/web.py	Wed Oct 04 09:11:58 2017 -0700
+++ b/piecrust/admin/web.py	Wed Oct 04 09:15:16 2017 -0700
@@ -7,7 +7,7 @@
 logger = logging.getLogger(__name__)
 
 
-def create_foodtruck_app(extra_settings=None):
+def create_foodtruck_app(extra_settings=None, url_prefix=None):
     from .blueprint import foodtruck_bp
 
     app = Flask(__name__.split('.')[0], static_folder=None)
@@ -15,17 +15,12 @@
     if extra_settings:
         app.config.update(extra_settings)
 
-    root_dir = app.config.setdefault('FOODTRUCK_ROOT', os.getcwd())
+    root_dir = app.config.setdefault('FOODTRUCK_ROOT_DIR', os.getcwd())
 
     app.config.from_pyfile(os.path.join(root_dir, 'admin_app.cfg'),
                            silent=True)
     app.config.from_envvar('FOODTRUCK_SETTINGS', silent=True)
 
-    # Add a special route for the `.well-known` directory.
-    app.wsgi_app = SharedDataMiddleware(
-        app.wsgi_app,
-        {'/.well-known': os.path.join(root_dir, '.well-known')})
-
     # Setup logging/error handling.
     if app.config['DEBUG']:
         l = logging.getLogger()
@@ -37,8 +32,32 @@
         app.config['SECRET_KEY'] = 'temp-key'
 
     # Register extensions and blueprints.
-    bp_prefix = app.config['FOODTRUCK_URL_PREFIX']
-    app.register_blueprint(foodtruck_bp, url_prefix=bp_prefix)
+    app.register_blueprint(foodtruck_bp, url_prefix=url_prefix)
+
+    # ---------------
+    # Debugging stuff
+    @app.errorhandler(404)
+    def page_not_found(e):
+        from flask import request, url_for
+        output = []
+        for rule in app.url_map.iter_rules():
+            options = {}
+            for arg in rule.arguments:
+                options[arg] = "[{0}]".format(arg)
+                methods = ','.join(rule.methods)
+                try:
+                    url = url_for(rule.endpoint, **options)
+                except:
+                    url = '???'
+                line = ("{:50s} {:20s} {}".format(rule.endpoint, methods, url))
+                output.append(line)
+
+        resp = request.path + '<br/>\n'
+        resp += str(request.environ) + '<br/>\n'
+        resp += str(app.config['FOODTRUCK_ROOT_URL']) + '<br/>\n'
+        resp += '<br/>\n'.join(sorted(output))
+        return resp, 404
+    # ---------------
 
     logger.debug("Created FoodTruck app with admin root: %s" % root_dir)