2020-06-17 20:18:43 -05:00
|
|
|
"""An application for running my Markdown-based sites."""
|
2020-03-06 19:53:29 -06:00
|
|
|
import logging
|
2020-03-06 17:51:20 -06:00
|
|
|
import os
|
2020-03-06 19:53:29 -06:00
|
|
|
from logging.config import dictConfig
|
2020-03-06 17:51:20 -06:00
|
|
|
|
2020-03-08 11:48:49 -05:00
|
|
|
from flask import Flask, request, send_from_directory
|
2020-03-06 17:51:20 -06:00
|
|
|
|
2020-03-07 00:24:54 -06:00
|
|
|
from ._version import get_versions
|
|
|
|
|
|
|
|
__version__ = get_versions()['version']
|
|
|
|
del get_versions
|
|
|
|
|
2020-03-06 17:51:20 -06:00
|
|
|
|
2020-03-07 09:24:20 -06:00
|
|
|
def create_app(instance_path=None, test_config=None):
|
2020-06-17 20:18:43 -05:00
|
|
|
"""Create the Flask app, with allowances for customizing path and test settings."""
|
2020-03-07 09:24:20 -06:00
|
|
|
app = Flask(__name__, instance_relative_config=True, instance_path=instance_path)
|
2020-03-06 17:51:20 -06:00
|
|
|
|
|
|
|
# if it doesn't already exist, create the instance folder
|
|
|
|
os.makedirs(app.instance_path, exist_ok=True)
|
|
|
|
|
2020-03-07 19:37:02 -06:00
|
|
|
# load defaults from config provided with the application
|
|
|
|
app.config.from_object('incorporealcms.config.Config')
|
|
|
|
# load specific instance configurations
|
|
|
|
app.config.from_pyfile('config.py', silent=True)
|
|
|
|
if test_config:
|
2020-03-06 17:51:20 -06:00
|
|
|
app.config.from_mapping(test_config)
|
|
|
|
|
2020-03-06 19:53:29 -06:00
|
|
|
dictConfig(app.config['LOGGING'])
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-03-07 09:24:20 -06:00
|
|
|
logger.debug("instance path: %s", app.instance_path)
|
|
|
|
|
2020-03-06 19:53:29 -06:00
|
|
|
@app.before_request
|
|
|
|
def log_request():
|
2020-03-15 18:43:15 -05:00
|
|
|
logger.info("REQUEST: %s %s", request.method, request.path)
|
|
|
|
|
|
|
|
@app.after_request
|
|
|
|
def log_response(response):
|
|
|
|
logger.info("RESPONSE: %s %s: %s", request.method, request.path, response.status)
|
|
|
|
return response
|
2020-03-06 19:53:29 -06:00
|
|
|
|
2020-03-08 11:48:49 -05:00
|
|
|
@app.route(f'/{app.config["MEDIA_DIR"]}/<path:filename>')
|
|
|
|
def media_files(filename):
|
|
|
|
return send_from_directory(os.path.join(app.instance_path, app.config['MEDIA_DIR']),
|
|
|
|
filename)
|
|
|
|
|
2020-03-07 14:06:57 -06:00
|
|
|
from . import pages
|
|
|
|
app.register_blueprint(pages.bp)
|
2020-03-06 18:08:24 -06:00
|
|
|
|
2020-03-06 17:51:20 -06:00
|
|
|
return app
|