aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/route_manager.py
blob: 6f973767283ce8dc0b699093d34247454b810ced (plain) (blame)
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
# coding=utf-8
import importlib
import inspect
import os

from flask import Blueprint, Flask, g

from pysite.base_route import APIView, BaseView, ErrorView, RouteView
from pysite.database import RethinkDB

TEMPLATES_PATH = "../templates"
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.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)

        # 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("")

        # Load the subdomains
        self.subdomains = ['api', 'staff']

        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", 8080)), debug="FLASK_DEBUG" in os.environ
        )

    def load_views(self, blueprint, location="pysite/views"):
        for filename in os.listdir(location):
            if os.path.isdir(f"{location}/{filename}"):
                # Recurse if it's a directory; load ALL the views!
                self.load_views(blueprint, location=f"{location}/{filename}")
                continue

            if filename.endswith(".py") and not filename.startswith("__init__"):
                module = importlib.import_module(f"{location}/{filename}".replace("/", ".")[:-3])

                for cls_name, cls in inspect.getmembers(module):
                    if (
                            inspect.isclass(cls) and
                            cls is not BaseView and
                            cls is not ErrorView and
                            cls is not RouteView and
                            cls is not APIView and
                            BaseView in cls.__mro__
                    ):
                        cls.setup(blueprint)
                        print(f">> View loaded: {cls.name: <15} ({module.__name__}.{cls_name})")