UnderCover/app/__init__.py

44 lines
1.4 KiB
Python
Raw Normal View History

# Copyright Sage Vaillancourt 2021
import os
2022-09-24 10:25:04 -04:00
import sys
from flask import Flask, Response, make_response, render_template
2022-09-27 21:55:10 -04:00
from flask_minify import Minify
from werkzeug.exceptions import HTTPException
2022-10-02 16:32:01 -04:00
from app import routes, email
def create_app(test_config=None) -> Flask:
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)
# if test_config is None:
# app.config.from_pyfile('config.py', silent=True)
# else:
# app.config.from_mapping(test_config)
os.makedirs(app.instance_path, exist_ok=True)
2022-10-02 16:25:23 -04:00
app.register_blueprint(routes.writing_blueprint)
@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)
return app