diff options
Diffstat (limited to 'pysite/route_manager.py')
-rw-r--r-- | pysite/route_manager.py | 76 |
1 files changed, 24 insertions, 52 deletions
diff --git a/pysite/route_manager.py b/pysite/route_manager.py index d7cf0fa1..6f973767 100644 --- a/pysite/route_manager.py +++ b/pysite/route_manager.py @@ -3,16 +3,10 @@ import importlib import inspect import os -from flask import Blueprint, Flask, abort, g - -import rethinkdb +from flask import Blueprint, Flask, g from pysite.base_route import APIView, BaseView, ErrorView, RouteView - -DB_HOST = os.environ.get("RETHINKDB_HOST") -DB_PORT = os.environ.get("RETHINKDB_PORT") -DB_DATABASE = os.environ.get("RETHINKDB_DATABASE") -DB_TABLE = os.environ.get("RETHINKDB_TABLE") +from pysite.database import RethinkDB TEMPLATES_PATH = "../templates" STATIC_PATH = "../static" @@ -20,29 +14,42 @@ STATIC_PATH = "../static" class RouteManager: def __init__(self): + + # Set up the app and the database self.app = Flask( __name__, template_folder=TEMPLATES_PATH, static_folder=STATIC_PATH, static_url_path="/static", ) - self.app.secret_key = os.environ.get("WEBPAGE_SECRET_KEY") - self.app.config["SERVER_NAME"] = os.environ.get("SERVER_NAME", "localhost") + self.db = RethinkDB() + self.app.secret_key = os.environ.get("WEBPAGE_SECRET_KEY", "super_secret") + self.app.config["SERVER_NAME"] = os.environ.get("SERVER_NAME", "pythondiscord.com:8080") + self.app.before_request(self.db.before_request) + self.app.teardown_request(self.db.teardown_request) - self.main_blueprint = Blueprint("main", __name__) + # Store the database in the Flask global context + with self.app.app_context(): + g.db = self.db # type: RethinkDB + # Load the main blueprint + self.main_blueprint = Blueprint("main", __name__) print(f"Loading Blueprint: {self.main_blueprint.name}") self.load_views(self.main_blueprint, "pysite/views/main") self.app.register_blueprint(self.main_blueprint) print("") - self.api_blueprint = Blueprint("api", __name__, subdomain="api") + # Load the subdomains + self.subdomains = ['api', 'staff'] - print(f"Loading Blueprint: {self.api_blueprint.name}") - self.load_views(self.api_blueprint, "pysite/views/api") - self.app.register_blueprint(self.api_blueprint) - print("") + for sub in self.subdomains: + self.sub_blueprint = Blueprint(sub, __name__, subdomain=sub) + + print(f"Loading Blueprint: {self.sub_blueprint.name}") + self.load_views(self.sub_blueprint, f"pysite/views/{sub}") + self.app.register_blueprint(self.sub_blueprint) + print("") def run(self): self.app.run( - port=int(os.environ.get("WEBPAGE_PORT")), debug="FLASK_DEBUG" in os.environ + port=int(os.environ.get("WEBPAGE_PORT", 8080)), debug="FLASK_DEBUG" in os.environ ) def load_views(self, blueprint, location="pysite/views"): @@ -66,38 +73,3 @@ class RouteManager: ): cls.setup(blueprint) print(f">> View loaded: {cls.name: <15} ({module.__name__}.{cls_name})") - - def setup_db(self): - connection = self.get_db_connection(connect_database=False) - - try: - rethinkdb.db_create(DB_DATABASE).run(connection) - rethinkdb.db(DB_DATABASE).table_create(DB_TABLE).run(connection) - print("Database created") - except rethinkdb.RqlRuntimeError: - print("Database found") - finally: - connection.close() - - self.app.before_request(self.db_before_request) - self.app.teardown_request(self.db_teardown_request) - - def get_db_connection(self, connect_database=True): - if connect_database: - return rethinkdb.connect(host=DB_HOST, port=DB_PORT, db=DB_DATABASE) - else: - return rethinkdb.connect(host=DB_HOST, port=DB_PORT) - - def db_before_request(self): - try: - # g is the Flask global context object - g.rdb_conn = self.get_db_connection() - except rethinkdb.RqlDriverError: - abort(503, "Database connection could be established.") - - def db_teardown_request(self, _): - try: - # g is the Flask global context object - g.rdb_conn.close() - except AttributeError: - pass |