diff --git a/.gitignore b/.gitignore index f187277..a2f9270 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__/ *.swp *.swo devices.json +config.py diff --git a/config.py.template b/config.py.template new file mode 100644 index 0000000..baa2a9c --- /dev/null +++ b/config.py.template @@ -0,0 +1,7 @@ +#!/usr/bin/env python3 +""" +Configuration settings for the Juice IOT hub server. +`url_prefix` is the root path you wish app to reside at +eg. https://example.com/juice. +""" +url_prefix = '/juice' diff --git a/juice.py b/juice.py index fd29590..df3bf9c 100644 --- a/juice.py +++ b/juice.py @@ -2,12 +2,16 @@ """ A hub for controlling IOT devices. """ +import os import re import copy import json import requests from flask import Flask, render_template, request, abort, jsonify +from flask import Blueprint + +import config class RelayDevice: @@ -127,11 +131,9 @@ def save_network(filepath="devices.json"): with open(filepath, 'w') as file: file.write(json.dumps(network, cls=DeviceEncoder, indent='\t')) +app_views = Blueprint('app_views', __name__) -app = Flask(__name__) -network = init_network() - -@app.route('/') +@app_views.route('/') def index(): """ The index page. @@ -139,7 +141,7 @@ def index(): return render_template('index.html', network=network) -@app.route('/toggle') +@app_views.route('/toggle') def toggle(): """ Toggles the state of a RelayDevice. @@ -158,7 +160,7 @@ def toggle(): save_network() return res -@app.route('/edit') +@app_views.route('/edit') def edit(): """ Edits the text of a particular field. @@ -208,5 +210,12 @@ def make_error(code, message): return res +app = Flask(__name__) +app.register_blueprint(app_views, url_prefix=config.url_prefix) +app.secret_key = os.urandom(32) +app.config['APPLICATION_ROOT'] = config.url_prefix +network = init_network() + + if __name__ == '__main__': app.run(host='0.0.0.0', port=5300)