-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
88 lines (80 loc) · 2.91 KB
/
__init__.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
Initialization of the app package. Regular Flask app variable creation with its modules.
"""
import logging
import os
from logging.handlers import RotatingFileHandler, SMTPHandler
from celery import Celery
from config import Config
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_login import LoginManager
from flask_mail import Mail
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
"""Initialize all components used by the app"""
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = "login" # function name for login
bootstrap = Bootstrap(app)
mail = Mail(app)
celery = Celery(
app.name,
broker=app.config["CELERY_BROKER_URL"],
backend=app.config["RESULT_BACKEND"],
)
celery.conf.update(app.config)
# we create the upload folder if not already existing
temp_upload_path = os.path.join(app.root_path, app.config["TEMPORARY_UPLOAD_FOLDER"])
if not os.path.exists(temp_upload_path):
os.mkdir(temp_upload_path)
upload_path = os.path.join(app.root_path, app.config["UPLOAD_FOLDER"])
if not os.path.exists(upload_path):
os.mkdir(upload_path)
"""Handles logging by mail and on files if we are not in debug mode"""
if not app.debug:
if app.config["MAIL_SERVER"] and not app.config["MAIL_USE_SSL"]:
# we cannot yet have mail logs with a SMTP over SSL connection
# https://docs.python.org/3/library/logging.handlers.html#logging.handlers.SMTPHandler
# There exists code to circumvent this https://github.com/dycw/ssl-smtp-handler but not widely adopted
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["ADMIN"],
subject="Secret Race Strolling Failure",
credentials=auth,
secure=secure,
)
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
if not os.path.exists("logs"):
os.mkdir("logs")
file_handler = RotatingFileHandler("logs/srs.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("Secret Race Strolling startup")
# import at the bottom to avoid circular dependencies
from app import (
cached_items,
errors,
models,
routes,
tasks_attack,
tasks_control,
tasks_defence,
)