aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/route_manager.py
diff options
context:
space:
mode:
authorGravatar lmn <[email protected]>2018-02-13 13:11:57 +0100
committerGravatar Joseph <[email protected]>2018-02-13 12:11:57 +0000
commitf92946e12f9acfa394c04a140a2a4d025a5fe3ca (patch)
treea74a0874c842952899f3d10ff2df8397fb89560d /pysite/route_manager.py
parentWebsocket echo test (diff)
RethinkDB API Views #yqhg
* Refactoring the database implementation into a class of its own. * Refactoring the database implementation into a class of its own. * healthcheck should belong to the API. * dynamic subdomain loading, setting up basic handling for staff.pythondiscord.com, and started on a TagView for a bot tag feature. * Oops, forgot to fix some merges. * Some quality of life updates - default values for env variables that aren't secret, and starting to get through the tag view. * Refactoring the database implementation into a class of its own. * healthcheck should belong to the API. * dynamic subdomain loading, setting up basic handling for staff.pythondiscord.com, and started on a TagView for a bot tag feature. * Oops, forgot to fix some merges. * Some quality of life updates - default values for env variables that aren't secret, and starting to get through the tag view. * API validation added to the APIView class, TagView should be finished as well. * super important commit you guys * fixed a bug with the RethinkDB class where host and port attributes were accessed before being created * Fixed my editor now you guys * Fixing up some of the problems brought up in gdude's review * Handling GET requests with param indata and POST with JSON. Fixed error handling to use the constants and the baseclass self.error(). * Get API-key from headers, context manage the db assignment, and default env var values * Changed API-KEY header to X_API_Key. Added a default for secret key. it should no longer be necessary with environment variables to run this system locally. * Changing back the nav to have relative paths * Why am I like this
Diffstat (limited to 'pysite/route_manager.py')
-rw-r--r--pysite/route_manager.py76
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