-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.py
More file actions
254 lines (210 loc) · 9.59 KB
/
App.py
File metadata and controls
254 lines (210 loc) · 9.59 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# Builtins
import json
from logging import info, warning, error, critical
import logging
from os import environ as env, makedirs, path, getcwd
# External
from flask import Flask
from werkzeug.serving import is_running_from_reloader
from flask_login import current_user
from waitress import serve
# Internal
from crypto import pw_hash
from connectors.discord import DiscordClient
from connectors.rss import RSSUpdateType
from framework.menu import navigation_menu
from framework.roles import role_badge, get_all_badges, RoleType, role_type_to_points, has_badge
from utils import ensure_config, config_has_key, DiscordErrorHandler
from tasks import discord_tasks, backup_task
from db import User
from crypto import generate_signing_keys
import db
from constants import APP_VERSION
# Blueprints
from blueprints.auth import AuthController
from blueprints.debug import DebugToolsController
from blueprints.content import ContentController
from blueprints.errorhandler import ErrorPageController
from blueprints.users import UserController
from blueprints.articles import ArticleController
from blueprints.stats import StatisticsController
from blueprints.api import ApiController
from blueprints.rsspage import RssPageController
from blueprints.oauth import OauthController
from blueprints.autobackup import AutobackupController
from blueprints.embed import EmbedController
from blueprints.leaderboard import LeaderboardController
from extensions import login_manager, sched, oauth, rss, webhook, portainer
app = Flask(__name__)
LOGGER_FORMAT_STR = '[%(asctime)s][%(module)s] %(levelname)s: %(message)s'
def init_logger() -> None:
"""
Sets up logging
"""
logging.getLogger().handlers.clear()
logging.basicConfig(filename='translatordb.log', filemode='a', format=LOGGER_FORMAT_STR, encoding='utf-8')
logging.getLogger().setLevel(logging.INFO)
handler_st = logging.StreamHandler()
handler_st.setFormatter(logging.Formatter(LOGGER_FORMAT_STR))
logging.getLogger().addHandler(handler_st)
def fix_proxy() -> None:
"""
Registers the ProxyFix middleware as described in https://flask.palletsprojects.com/en/3.0.x/deploying/proxy_fix/
"""
if "FIX_PROXY" in app.config and app.config['FIX_PROXY']:
from werkzeug.middleware.proxy_fix import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1)
info("Registered ProxyFix middleware")
def user_init() -> None:
"""
Registers an administrator from environment variables
"""
init_user, init_password = env.get("SCP_INIT_USER"), env.get("SCP_INIT_PASSWORD")
if not init_user:
return
if not init_password:
error(f"Password not specified for {init_user}")
exit(1)
if User.get_or_none(User.nickname == init_user) is not None:
warning(f"Initial user {init_user} already exists")
return
info(f"Adding initial user {init_user}")
admin = User.create(nickname=init_user, password=pw_hash(init_password), discord="", wikidot="")
# TODO: Do something with this id
def extensions_init() -> None:
"""
Checks which integrations can be enabled, initializes all flask extensions and schedules background tasks
"""
# Set up login manager
login_manager.session_protection = "basic"
login_manager.login_view = "AuthController.login"
login_manager.login_message = u"Pro zobrazení této stránky se přihlaste"
login_manager.user_loader(lambda uid: User.get_by_id(uid))
login_manager.init_app(app)
# Checking if we can enable Discord Login
if config_has_key(app.config, 'DISCORD.CLIENT_ID')\
and config_has_key(app.config, 'DISCORD.CLIENT_SECRET')\
and config_has_key(app.config, 'DISCORD.REDIRECT_URI'):
app.config['OAUTH_ENABLE'] = app.config['DISCORD'].get('LOGIN_ENABLE', True)
# Set the config keys that the OAuth extension requires
app.config['DISCORD_CLIENT_ID'] = app.config['DISCORD']['CLIENT_ID']
app.config['DISCORD_CLIENT_SECRET'] = app.config['DISCORD']['CLIENT_SECRET']
app.config['DISCORD_REDIRECT_URI'] = app.config['DISCORD']['REDIRECT_URI']
else:
warning('OAuth App ID or secret not set, Discord login disabled')
app.config['OAUTH_ENABLE'] = False
if app.config['OAUTH_ENABLE']:
oauth.init_app(app)
# Check if we are running inside Flask's auto reloader yet
# This doesn't matter when deployed but can break things when debugging
if is_running_from_reloader() or not app.config['DEBUG']:
sched.init_app(app)
sched.start()
# Checking if we can enable the API connection
if config_has_key(app.config, 'DISCORD.TOKEN'):
DiscordClient.init_app(app)
sched.add_job('Download avatars', lambda: discord_tasks.download_avatars_task(), trigger='interval', days=3)
sched.add_job('Fetch nicknames', lambda: discord_tasks.update_nicknames_task(), trigger='interval', days=4)
else:
warning('Discord API token not set. Profiles won\'t be updated!')
if app.config.get('BACKUP', {}).get('BACKUP_INTERVAL') is not None:
sched.add_job('autobackup_run', lambda: backup_task.run_backup_task(app.config['BACKUP']['BACKUP_INTERVAL'], app), trigger='interval', hours=12)
# Checking if we have a webhook URL
if config_has_key(app.config, 'WEBHOOK.WEBHOOK_URL') and config_has_key(app.config, 'DISCORD_ROLEMASTER_ID'):
webhook.init_app(app)
app.config['WEBHOOK_ENABLE'] = True
handler_discord = DiscordErrorHandler()
handler_discord.setFormatter(logging.Formatter(LOGGER_FORMAT_STR))
handler_discord.set_webhook(webhook)
logging.getLogger().addHandler(handler_discord)
else:
app.config['WEBHOOK_ENABLE'] = False
rss.init_app(app)
# Checking if we have any RSS feeds configured
if rss.has_links:
sched.add_job('Fetch RSS updates', rss.check, trigger='interval', hours=1)
# Check if Portainer config is present
if config_has_key(app.config, 'BACKUP.PORTAINER'):
portainer.init_app(app)
def create_directories(app: Flask) -> None:
"""
Creates all directories needed for the app
"""
info("Creating directories")
current_dir = getcwd()
# Ensure we have a directory to store the avatar thumbnails
makedirs(path.join(current_dir, 'temp', 'avatar'), exist_ok=True)
# Ensure we have a directory to store original site snapshots
makedirs(path.join(current_dir, 'temp', 'snapshots'), exist_ok=True)
# Make snapshot directories for each source wiki
if 'MONITORED_WIKIS' in app.config and config_has_key(app.config, "BACKUP.save_snapshots", True):
for wiki in app.config['MONITORED_WIKIS']:
makedirs(path.join(current_dir, 'temp', 'snapshots', wiki['source_wiki']), exist_ok=True)
# Create a data directory if it doesn't exist
# Regular mkdir doesn't have the exist_ok option for whatever reason
makedirs(path.join(current_dir, 'data'), exist_ok=True)
def register_blueprints(app: Flask) -> None:
# Load all the blueprints
app.register_blueprint(LeaderboardController)
app.register_blueprint(ErrorPageController)
app.register_blueprint(ContentController)
app.register_blueprint(AuthController)
app.register_blueprint(DebugToolsController)
app.register_blueprint(UserController)
app.register_blueprint(ArticleController)
app.register_blueprint(StatisticsController)
app.register_blueprint(ApiController)
app.register_blueprint(OauthController)
app.register_blueprint(RssPageController)
app.register_blueprint(AutobackupController)
app.register_blueprint(EmbedController)
def register_template_globals(app: Flask) -> None:
# Add useful template globals
app.add_template_global(current_user, 'current_user')
app.add_template_global(RSSUpdateType)
app.add_template_global(RoleType)
app.add_template_global(navigation_menu)
app.add_template_global(role_badge)
app.add_template_global(has_badge)
app.add_template_global(get_all_badges)
app.add_template_global(role_type_to_points)
app.add_template_global(APP_VERSION, 'APP_VERSION')
# TODO: App factory??
if __name__ == '__main__':
init_logger()
# Set debug logging level before doing anything else
if app.config['DEBUG']:
logging.getLogger().setLevel(logging.DEBUG)
info(f"SCUTTLE v{APP_VERSION} starting up")
# Load config file or create it if there isn't one
if not ensure_config('config.json') or not app.config.from_file('config.json', json.load):
critical("Config file is inaccessible, malformed or could not be created")
exit(1)
create_directories(app)
# Store all the singleton classes in config to access them from blueprints
# TODO: do something abt this
app.config['webhook'] = webhook
register_template_globals(app)
register_blueprints(app)
# Initialize the database
db.database.connect()
db.database.create_tables(db.models)
db.create_views(db.database)
# Create the admin user
user_init()
# Generate signing keys
if not generate_signing_keys():
critical("Error while generating signing keys. Exiting...")
exit(1)
# Load extensions and enable integrations based on config
extensions_init()
# Force oauthlib to allow insecure transport when debugging
if app.config['DEBUG']:
warning('App running in debug mode!')
env['OAUTHLIB_INSECURE_TRANSPORT'] = 'true'
warning('OAUTHLIB insecure transport is enabled!')
app.run('0.0.0.0', 8080, debug=True)
else:
fix_proxy()
info("Init complete. Starting WSGI server now.")
serve(app, threads=64)