diff --git a/backend.py b/backend.py new file mode 100644 index 0000000..1174128 --- /dev/null +++ b/backend.py @@ -0,0 +1,15 @@ +""" +Imports all required blueprints and initalises the app. +Runs in debug mode if ran directly. +""" + +import flask + +from static import staticBP + +app = flask.Flask(__name__, static_folder='./www/') +app.register_blueprint(staticBP) + +if __name__ == '__main__': + print('Running in debug mode') + app.run(debug=True) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2077213 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +Flask \ No newline at end of file diff --git a/static.py b/static.py new file mode 100644 index 0000000..b10ccd7 --- /dev/null +++ b/static.py @@ -0,0 +1,26 @@ +""" +Holds the blueprint for serving the static folder. +THIS WILL ONLY BE FUNCTIONAL IN DEBUG MODE, as Nginx should serve the static folder in production. +""" + +import os + +import flask + +staticBP = flask.Blueprint('staticPage', __name__, static_folder='./www/') + +@staticBP.route('/', defaults={'path': 'index.html'}) +@staticBP.route('/') +def staticPage(path): + """ + Returns anything requested in the static folder. + """ + + if not flask.current_app.debug: + # Return forbidden code if trying to access static files in production mode. + flask.abort(403) + + if os.path.isfile(os.path.join(staticBP.static_folder, path, 'index.html')): + path = os.path.join(path, 'index.html') + + return flask.send_from_directory(staticBP.static_folder, path)