forked from RealTimeWeb/Blockpy-Server
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.py
More file actions
108 lines (89 loc) · 3.63 KB
/
main.py
File metadata and controls
108 lines (89 loc) · 3.63 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
# Built-ins
import json
import os
# Flask
from common.flask_extensions import CustomFlask
from flask import Flask
from flask_debugtoolbar import DebugToolbarExtension
from flask_jwt_extended import JWTManager
from werkzeug.middleware.proxy_fix import ProxyFix
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 = CustomFlask('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':
print("Loading TestConfig")
app.config.from_object('config.TestConfig')
else:
print("Loading custom test config")
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')
# Load the instance config, if it exists
if isinstance(instance_config, dict):
if '_BASE_FILE' in instance_config:
base_file = instance_config['_BASE_FILE']
print(f"Loading base instance configuration from {base_file!r}")
app.config.from_pyfile(base_file)
del instance_config['_BASE_FILE']
print("Loading instance configuration from dict")
app.config.from_mapping(instance_config)
else:
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')
if app.config['USE_PROXY_FIX']:
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_port=1)
# 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 mailing import setup_mail
setup_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