#!/usr/bin/env python3 import logging from flask import Flask from flask_restful import Api import sentry_sdk from sentry_sdk.integrations.flask import FlaskIntegration from config import * from db import db from marshm import ma from mqtt_flask_instance import mqtt """ Main Flask RESTful API """ __author__ = "@tormakris" __copyright__ = "Copyright 2020, Birbnetes Team" __module_name__ = "app" __version__text__ = "1" if SENTRY_DSN: sentry_sdk.init( dsn=SENTRY_DSN, integrations=[FlaskIntegration()], send_default_pii=True, release=RELEASE_ID, environment=RELEASEMODE ) app = Flask(__name__) app.config['MQTT_BROKER_URL'] = MQTT_HOSTNAME app.config['MQTT_BROKER_PORT'] = MQTT_PORT app.config['MQTT_USERNAME'] = MQTT_USERNAME app.config['MQTT_PASSWORD'] = MQTT_PASSWORD app.config['MQTT_REFRESH_TIME'] = 1.0 # refresh time in seconds app.config['SQLALCHEMY_DATABASE_URI'] = f"postgresql://{POSTGRES_USERNAME}:{POSTGRES_PASSWORD}@{POSTGRES_HOSTNAME}:5432/{POSTGRES_DB}" api = Api(app) db.init_app(app) ma.init_app(app) mqtt.init_app(app) with app.app_context(): db.create_all() formatter = logging.Formatter( fmt="%(asctime)s - %(levelname)s - %(module)s - %(message)s" ) handler = logging.StreamHandler() handler.setFormatter(formatter) logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) logger.addHandler(handler) @mqtt.on_log() def handle_logging(client, userdata, level, buf): logger.log(level, buf) @mqtt.on_connect() def handle_connect(client, userdata, flags, rc): mqtt.subscribe(MQTT_STATUS_TOPIC) # api.add_resource(SampleResource, "/sample") # api.add_resource(SampleParameterResource, '/sample/') if __name__ == "__main__": app.run( debug=bool(DEBUG), host="0.0.0.0", use_reloader=False, port=int(PORT), )