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
|
|
|
|
2021-04-17 11:16:34 -05:00
|
|
|
from flask import Flask, request
|
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
|
|
|
|
2021-02-21 19:35:52 -06:00
|
|
|
from . import error_pages, pages
|
2020-03-07 14:06:57 -06:00
|
|
|
app.register_blueprint(pages.bp)
|
2021-02-21 19:35:52 -06:00
|
|
|
app.register_error_handler(400, error_pages.bad_request)
|
|
|
|
app.register_error_handler(404, error_pages.page_not_found)
|
|
|
|
app.register_error_handler(500, error_pages.internal_server_error)
|
2020-03-06 18:08:24 -06:00
|
|
|
|
2020-03-06 17:51:20 -06:00
|
|
|
return app
|