diff --git a/.gitignore b/.gitignore index fee4ec0..8fd89ef 100644 --- a/.gitignore +++ b/.gitignore @@ -143,4 +143,5 @@ cython_debug/ .vscode/ # the-hunt specific: +logs/ app.db \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py index 0c61044..8b3f4ce 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,10 +1,16 @@ +from config import Config +import logging +from logging.handlers import SMTPHandler +from logging.handlers import RotatingFileHandler +import os + from flask import Flask from flask_bootstrap import Bootstrap -from config import Config from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate from flask_login import LoginManager + app = Flask(__name__) app.config.from_object(Config) bootstrap = Bootstrap(app) @@ -13,4 +19,39 @@ migrate = Migrate(app, db) login = LoginManager(app) login.login_view = 'login' -from app import routes, models +from app import routes, models, errors + +if not app.debug and not app.testing: + if app.config['MAIL_SERVER']: + auth = None + if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']: + auth = (app.config['MAIL_USERNAME'], + app.config['MAIL_PASSWORD']) + secure = None + if app.config['MAIL_USE_TLS']: + secure = () + mail_handler = SMTPHandler( + mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']), + fromaddr='no-reply@' + app.config['MAIL_SERVER'], + toaddrs=app.config['ADMINS'], subject='Microblog Failure', + credentials=auth, secure=secure) + mail_handler.setLevel(logging.ERROR) + app.logger.addHandler(mail_handler) + + if app.config['LOG_TO_STDOUT']: + stream_handler = logging.StreamHandler() + stream_handler.setLevel(logging.INFO) + app.logger.addHandler(stream_handler) + else: + if not os.path.exists('logs'): + os.mkdir('logs') + file_handler = RotatingFileHandler('logs/microblog.log', + maxBytes=10240, backupCount=10) + file_handler.setFormatter(logging.Formatter( + '%(asctime)s %(levelname)s: %(message)s ' + '[in %(pathname)s:%(lineno)d]')) + file_handler.setLevel(logging.INFO) + app.logger.addHandler(file_handler) + + app.logger.setLevel(logging.INFO) + app.logger.info('Microblog startup') \ No newline at end of file diff --git a/app/errors.py b/app/errors.py new file mode 100644 index 0000000..1fa9df9 --- /dev/null +++ b/app/errors.py @@ -0,0 +1,11 @@ +from flask import render_template +from app import app, db + +@app.errorhandler(404) +def not_found_error(error): + return render_template('404.html'), 404 + +@app.errorhandler(500) +def internal_error(error): + db.session.rollback() + return render_template('500.html'), 500 \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index c306e44..1a4c9fd 100644 --- a/app/routes.py +++ b/app/routes.py @@ -66,4 +66,4 @@ def game_dashboard(game_name): @login_required @app.route('/game//player/') def game_player(game_name, player_name): - return render_template("index.html", title='Home') \ No newline at end of file + return redirect(url_for('indsex')) \ No newline at end of file diff --git a/app/templates/404.html b/app/templates/404.html new file mode 100644 index 0000000..b76b06c --- /dev/null +++ b/app/templates/404.html @@ -0,0 +1,6 @@ +{% extends "base.html" %} + +{% block app_content %} +

File Not Found

+

Back

+{% endblock %} \ No newline at end of file diff --git a/app/templates/500.html b/app/templates/500.html new file mode 100644 index 0000000..88addac --- /dev/null +++ b/app/templates/500.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} + +{% block app_content %} +

An unexpected error has occurred

+

The administrator has been notified. Sorry for the inconvenience!

+

Back

+{% endblock %} \ No newline at end of file diff --git a/config.py b/config.py index b6ed349..80caac9 100644 --- a/config.py +++ b/config.py @@ -1,9 +1,19 @@ import os +from dotenv import load_dotenv from pathlib import Path basedir = Path(__file__).parent.absolute() +load_dotenv(basedir / '.env') class Config(object): SECRET_KEY = os.environ.get('SECRET_KEY') or 'you-will-never-guess' SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \ f"sqlite:///{Path(basedir) / 'app.db'}" SQLALCHEMY_TRACK_MODIFICATIONS = False + + LOG_TO_STDOUT = os.environ.get('LOG_TO_STDOUT') + MAIL_SERVER = os.environ.get('MAIL_SERVER') + MAIL_PORT = int(os.environ.get('MAIL_PORT') or 25) + MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') is not None + MAIL_USERNAME = os.environ.get('MAIL_USERNAME') + MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') + ADMINS = ['your-email@example.com'] \ No newline at end of file