-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
66 lines (49 loc) · 1.48 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import uuid
import constants
import json
from flask import Flask, session
from api import api
import logging
from logging.handlers import SMTPHandler
app = Flask(__name__)
app.register_blueprint(api)
#
# Generates a CSRF token to protect against un-authorized requests
#
def generate_csrf_token():
if '_csrf_token' not in session:
session['_csrf_token'] = str(uuid.uuid4())
return session['_csrf_token']
#
# Enables logging that sends error reports to admins
#
def enable_logging():
if not app.debug:
mail_handler = SMTPHandler(
mailhost=(constants.SMTP_SERVER, constants.SMTP_PORT),
fromaddr=constants.SMTP_EMAIL,
toaddrs=constants.ADMIN,
subject='Your Application Failed',
credentials=(constants.SMTP_EMAIL, constants.SMTP_PASSWORD),
secure=())
mail_handler.setLevel(logging.ERROR)
logging.getLogger().addHandler(mail_handler)
#
# Loads config values for app from file
#
def load_config(flask_app):
try:
# Read config file
config_file = open(constants.CONFIG_FILENAME)
config_file_contents = config_file.read()
# Initialize config via file
config = json.loads(config_file_contents)
constants.init(config)
flask_app.secret_key = config["secret_key"]
except:
return
if __name__ == '__main__':
load_config(app)
enable_logging()
app.jinja_env.globals['csrf_token'] = generate_csrf_token
app.run()