Added static backend
This commit is contained in:
15
backend.py
Normal file
15
backend.py
Normal file
@ -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)
|
||||||
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
Flask
|
||||||
26
static.py
Normal file
26
static.py
Normal file
@ -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('/<path:path>')
|
||||||
|
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)
|
||||||
Reference in New Issue
Block a user