2022-09-23 11:35:56 -04:00
|
|
|
# Copyright Sage Vaillancourt 2021
|
|
|
|
|
|
|
|
import os
|
2022-09-24 10:25:04 -04:00
|
|
|
import sys
|
2022-09-23 11:35:56 -04:00
|
|
|
|
2022-10-02 15:09:16 -04:00
|
|
|
from flask import Flask, Response, make_response, render_template
|
2022-09-27 21:55:10 -04:00
|
|
|
from flask_minify import Minify
|
2022-10-02 15:09:16 -04:00
|
|
|
from werkzeug.exceptions import HTTPException
|
2022-09-23 11:35:56 -04:00
|
|
|
|
2022-10-02 16:32:01 -04:00
|
|
|
from app import routes, email
|
2022-09-23 11:35:56 -04:00
|
|
|
|
|
|
|
|
2022-09-28 17:53:32 -04:00
|
|
|
def create_app(test_config=None) -> Flask:
|
2022-09-23 11:35:56 -04:00
|
|
|
app = Flask(__name__, instance_relative_config=True)
|
2022-09-27 21:55:10 -04:00
|
|
|
Minify(app=app, html=True, js=True, cssless=True)
|
2022-10-02 22:18:55 -04:00
|
|
|
|
2022-09-24 10:25:04 -04:00
|
|
|
secret_key = os.environ.get('UNDERCOVER_SECRET_KEY')
|
|
|
|
if not secret_key:
|
|
|
|
sys.stderr.write("WARNING: UNDERCOVER_SECRET_KEY is not defined! Application may be insecure.\n")
|
|
|
|
secret_key = "dev"
|
2022-10-02 16:25:23 -04:00
|
|
|
app.config.from_mapping(SECRET_KEY=secret_key)
|
2022-09-23 11:35:56 -04:00
|
|
|
|
2022-09-23 13:59:43 -04:00
|
|
|
# if test_config is None:
|
|
|
|
# app.config.from_pyfile('config.py', silent=True)
|
|
|
|
# else:
|
|
|
|
# app.config.from_mapping(test_config)
|
2022-09-23 11:35:56 -04:00
|
|
|
|
|
|
|
os.makedirs(app.instance_path, exist_ok=True)
|
|
|
|
|
2022-10-02 16:25:23 -04:00
|
|
|
app.register_blueprint(routes.writing_blueprint)
|
|
|
|
|
2022-10-02 15:09:16 -04:00
|
|
|
@app.errorhandler(Exception)
|
|
|
|
def internal_error(e) -> HTTPException | Response:
|
|
|
|
if isinstance(e, HTTPException):
|
|
|
|
return e
|
|
|
|
email.exception_alert(e)
|
|
|
|
return make_response(render_template('error.jinja2', status=500, error_text='Internal error occurred.', extra_text="We're looking into it!"), 500)
|
|
|
|
|
|
|
|
@app.errorhandler(404)
|
|
|
|
def not_found(e) -> Response:
|
|
|
|
return make_response(render_template('error.jinja2', status=404, error_text='Page not found!'), 404)
|
|
|
|
|
2022-09-23 11:35:56 -04:00
|
|
|
return app
|