Browse Source

Implement Issue #1 and add logging

feature_tests
Burathar 5 years ago
parent
commit
4af9ec36af
  1. 1
      .gitignore
  2. 45
      app/__init__.py
  3. 11
      app/errors.py
  4. 2
      app/routes.py
  5. 6
      app/templates/404.html
  6. 7
      app/templates/500.html
  7. 10
      config.py

1
.gitignore vendored

@ -143,4 +143,5 @@ cython_debug/ @@ -143,4 +143,5 @@ cython_debug/
.vscode/
# the-hunt specific:
logs/
app.db

45
app/__init__.py

@ -1,10 +1,16 @@ @@ -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) @@ -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')

11
app/errors.py

@ -0,0 +1,11 @@ @@ -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

2
app/routes.py

@ -66,4 +66,4 @@ def game_dashboard(game_name): @@ -66,4 +66,4 @@ def game_dashboard(game_name):
@login_required
@app.route('/game/<game_name>/player/<player_name>')
def game_player(game_name, player_name):
return render_template("index.html", title='Home')
return redirect(url_for('indsex'))

6
app/templates/404.html

@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
{% extends "base.html" %}
{% block app_content %}
<h1>File Not Found</h1>
<p><a href="{{ url_for('index') }}">Back</a></p>
{% endblock %}

7
app/templates/500.html

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
{% extends "base.html" %}
{% block app_content %}
<h1>An unexpected error has occurred</h1>
<p>The administrator has been notified. Sorry for the inconvenience!</p>
<p><a href="{{ url_for('index') }}">Back</a></p>
{% endblock %}

10
config.py

@ -1,9 +1,19 @@ @@ -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']
Loading…
Cancel
Save