Make devices module export a blueprint for better modularity

master
esensar 2018-04-26 16:45:28 +02:00
parent 45e63255eb
commit 5018fc10e2
4 changed files with 27 additions and 18 deletions

View File

@ -1,21 +1,12 @@
import atexit
from flask import Flask from flask import Flask
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from app.mod_devices import setup_mqtt, tear_down_mqtt from app.mod_devices import setup_mqtt, tear_down_mqtt, devices
app = Flask(__name__, instance_relative_config=True) app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config') app.config.from_object('config')
app.config.from_pyfile('config.py') app.config.from_pyfile('config.py')
db = SQLAlchemy(app) db = SQLAlchemy(app)
app.register_blueprint(devices, url_prefix='/devices')
def on_stop():
print('Application stopping')
tear_down_mqtt()
setup_mqtt(app)
atexit.register(on_stop)
@app.route("/") @app.route("/")

View File

@ -1,10 +1,23 @@
import atexit
from flask import Blueprint
from flask_mqtt import Mqtt from flask_mqtt import Mqtt
devices = Blueprint('devices', __name__)
mqtt = Mqtt() mqtt = Mqtt()
def on_stop():
tear_down_mqtt()
def setup_mqtt(app): atexit.register(on_stop)
mqtt.init_app(app)
@devices.route("/")
def hello():
return "Hello from devices!"
@devices.record
def setup_mqtt(setup_state):
mqtt.init_app(setup_state.app)
mqtt.client.on_message = handle_mqtt_message mqtt.client.on_message = handle_mqtt_message
mqtt.client.on_subscribe = handle_subscribe mqtt.client.on_subscribe = handle_subscribe
print('MQTT client initialized') print('MQTT client initialized')

View File

@ -6,7 +6,7 @@ import os
BASE_DIR = os.path.abspath(os.path.dirname(__file__)) BASE_DIR = os.path.abspath(os.path.dirname(__file__))
# Define the database - we are working with # Define the database - we are working with
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(BASE_DIR, 'app.db') SQLALCHEMY_DATABASE_URI = os.environ['DATABASE_URL']
DATABASE_CONNECT_OPTIONS = {} DATABASE_CONNECT_OPTIONS = {}
# Application threads. A common general assumption is # Application threads. A common general assumption is

View File

@ -1,13 +1,18 @@
alembic==0.9.9
click==6.7 click==6.7
Flask==0.12.2 Flask==0.12.2
Flask-Migrate==2.1.1
Flask-MQTT==1.0.3 Flask-MQTT==1.0.3
greenlet==0.4.13 Flask-SQLAlchemy==2.3.2
itsdangerous==0.24 itsdangerous==0.24
Jinja2==2.10 Jinja2==2.10
Mako==1.0.7
MarkupSafe==1.0 MarkupSafe==1.0
msgpack==0.5.6
neovim==0.2.4
paho-mqtt==1.3.1 paho-mqtt==1.3.1
psycopg2==2.7.4
python-dateutil==2.7.2
python-editor==1.0.3
six==1.11.0
SQLAlchemy==1.2.7
typing==3.6.4 typing==3.6.4
virtualenv==15.2.0
Werkzeug==0.14.1 Werkzeug==0.14.1