UnderCover/undercover/__init__.py

48 lines
1.5 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
import undercover.routes
import undercover.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-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"
app.config.from_mapping(
2022-09-24 10:25:04 -04:00
SECRET_KEY=secret_key,
# DATABASE=os.path.join(app.instance_path, 'undercover.sqlite'),
)
# 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)
@app.errorhandler(Exception)
def internal_error(e) -> HTTPException | Response:
if isinstance(e, HTTPException):
return e
print(str(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.register_blueprint(routes.writing_blueprint)
@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