comparison 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
comparison
equal deleted inserted replaced
934:98430e7143d2 935:7ecb946bfafd
5 5
6 6
7 logger = logging.getLogger(__name__) 7 logger = logging.getLogger(__name__)
8 8
9 9
10 def create_foodtruck_app(extra_settings=None): 10 def create_foodtruck_app(extra_settings=None, url_prefix=None):
11 from .blueprint import foodtruck_bp 11 from .blueprint import foodtruck_bp
12 12
13 app = Flask(__name__.split('.')[0], static_folder=None) 13 app = Flask(__name__.split('.')[0], static_folder=None)
14 app.config.from_object('piecrust.admin.settings') 14 app.config.from_object('piecrust.admin.settings')
15 if extra_settings: 15 if extra_settings:
16 app.config.update(extra_settings) 16 app.config.update(extra_settings)
17 17
18 root_dir = app.config.setdefault('FOODTRUCK_ROOT', os.getcwd()) 18 root_dir = app.config.setdefault('FOODTRUCK_ROOT_DIR', os.getcwd())
19 19
20 app.config.from_pyfile(os.path.join(root_dir, 'admin_app.cfg'), 20 app.config.from_pyfile(os.path.join(root_dir, 'admin_app.cfg'),
21 silent=True) 21 silent=True)
22 app.config.from_envvar('FOODTRUCK_SETTINGS', silent=True) 22 app.config.from_envvar('FOODTRUCK_SETTINGS', silent=True)
23
24 # Add a special route for the `.well-known` directory.
25 app.wsgi_app = SharedDataMiddleware(
26 app.wsgi_app,
27 {'/.well-known': os.path.join(root_dir, '.well-known')})
28 23
29 # Setup logging/error handling. 24 # Setup logging/error handling.
30 if app.config['DEBUG']: 25 if app.config['DEBUG']:
31 l = logging.getLogger() 26 l = logging.getLogger()
32 l.setLevel(logging.DEBUG) 27 l.setLevel(logging.DEBUG)
35 # If there's no secret key, create a temp one but mark the app as not 30 # If there's no secret key, create a temp one but mark the app as not
36 # correctly installed so it shows the installation information page. 31 # correctly installed so it shows the installation information page.
37 app.config['SECRET_KEY'] = 'temp-key' 32 app.config['SECRET_KEY'] = 'temp-key'
38 33
39 # Register extensions and blueprints. 34 # Register extensions and blueprints.
40 bp_prefix = app.config['FOODTRUCK_URL_PREFIX'] 35 app.register_blueprint(foodtruck_bp, url_prefix=url_prefix)
41 app.register_blueprint(foodtruck_bp, url_prefix=bp_prefix) 36
37 # ---------------
38 # Debugging stuff
39 @app.errorhandler(404)
40 def page_not_found(e):
41 from flask import request, url_for
42 output = []
43 for rule in app.url_map.iter_rules():
44 options = {}
45 for arg in rule.arguments:
46 options[arg] = "[{0}]".format(arg)
47 methods = ','.join(rule.methods)
48 try:
49 url = url_for(rule.endpoint, **options)
50 except:
51 url = '???'
52 line = ("{:50s} {:20s} {}".format(rule.endpoint, methods, url))
53 output.append(line)
54
55 resp = request.path + '<br/>\n'
56 resp += str(request.environ) + '<br/>\n'
57 resp += str(app.config['FOODTRUCK_ROOT_URL']) + '<br/>\n'
58 resp += '<br/>\n'.join(sorted(output))
59 return resp, 404
60 # ---------------
42 61
43 logger.debug("Created FoodTruck app with admin root: %s" % root_dir) 62 logger.debug("Created FoodTruck app with admin root: %s" % root_dir)
44 63
45 return app 64 return app
46 65