forked from RealTimeWeb/Blockpy-Server
-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.py
89 lines (73 loc) · 2.85 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Built-ins
import json
import os
# Flask
from flask import Flask
from flask_debugtoolbar import DebugToolbarExtension
from flask_jwt_extended import JWTManager
def create_app(test_config=None, instance_config="configuration.py") -> Flask:
"""
Per the App Factory pattern, this creates a new instance of the BlockPy app.
:param test_config: 'testing' for unit tests, or a specific config object.
:return: a new instance of the BlockPy app.
"""
# create and configure the app
instance_path = os.path.join(os.path.dirname(__file__), './instance')
app = Flask('blockpy', instance_relative_config=True, static_folder='static',
instance_path=instance_path)
#static_url_path='/static')
# load the test config if passed in
if test_config is not None:
if test_config == 'testing':
app.config.from_object('config.TestConfig')
else:
app.config.from_mapping(test_config)
elif app.config['DEBUG']:
app.config.from_object('config.DevelopmentConfig')
else:
print("Loading Production!")
app.config.from_object('config.ProductionConfig')
print(f"Loading instance configuration from {instance_config!r}")
app.config.from_pyfile(instance_config)
# Additional settings being overridden here
app.config['TEMPLATES_AUTO_RELOAD'] = True
# Turn on profiler if necessary
if app.config['PROFILE_RUNTIME']:
from werkzeug.middleware.profiler import ProfilerMiddleware
app.wsgi_app = ProfilerMiddleware(app.wsgi_app, stream=None,
profile_dir=app.config['TIMING_LOG_DIR'],
filename_format='{method}.{path}.{elapsed:.0f}ms.{time:.0f}.pstat')
# Ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
# Load up the database
# TODO: Finish init_database
from models import init_database
init_database(app)
from tasks.setup import setup_tasks
huey = setup_tasks(app.config['TASK_QUEUE_STYLE'],
app.config['TASK_DB_URI'],
app.config['TASK_DB_SETTINGS'])
app.huey = huey
# Turn on Debug Toolbar
app.debug_toolbar = DebugToolbarExtension(app)
# Set Up JWT
jwt = JWTManager(app)
# Email
from flask_mail import Mail
mail = Mail(app)
# Set up all the endpoints
with app.app_context():
# Modify Jinja2
from controllers.jinja_filters import setup_jinja_filters
setup_jinja_filters(app)
# Logging
from controllers.interaction_logger import setup_logging
setup_logging(app)
# Load up all the controllers
from controllers import create_blueprints
create_blueprints(app)
print("Loaded all endpoints successfully!")
return app