cnc-service/src/app.py

95 lines
2.9 KiB
Python
Raw Normal View History

2020-07-17 16:31:49 +02:00
#!/usr/bin/env python3
2020-09-20 02:14:41 +02:00
import logging
2020-07-17 16:31:49 +02:00
from flask import Flask
from flask_restful import Api
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
2020-07-30 16:00:28 +02:00
from sentry_sdk.integrations.logging import LoggingIntegration
2020-10-19 22:26:49 +02:00
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
2020-11-19 01:54:16 +01:00
from healthcheck import HealthCheck
2020-07-17 16:31:49 +02:00
2020-07-26 17:45:19 +02:00
from resources import *
2020-07-17 16:31:49 +02:00
from config import *
from db import db
from marshm import ma
2020-07-20 17:09:05 +02:00
from mqtt_flask_instance import mqtt
from mqtt_methods import handle_status_message
2020-11-19 01:54:16 +01:00
from healthchecks import health_database_status
2020-09-19 16:35:21 +02:00
2020-07-17 16:31:49 +02:00
"""
Main Flask RESTful API
"""
__author__ = "@tormakris"
__copyright__ = "Copyright 2020, Birbnetes Team"
__module_name__ = "app"
__version__text__ = "1"
if SENTRY_DSN:
2020-07-30 16:00:28 +02:00
sentry_logging = LoggingIntegration(
level=logging.DEBUG,
event_level=logging.ERROR
)
2020-07-17 16:31:49 +02:00
sentry_sdk.init(
dsn=SENTRY_DSN,
2020-10-19 22:26:49 +02:00
integrations=[FlaskIntegration(), sentry_logging, SqlalchemyIntegration()],
2020-07-30 16:00:28 +02:00
traces_sample_rate=1.0,
2020-07-17 16:31:49 +02:00
send_default_pii=True,
release=RELEASE_ID,
2020-07-30 16:00:28 +02:00
environment=RELEASEMODE,
_experiments={"auto_enabling_integrations": True}
2020-07-17 16:31:49 +02:00
)
app = Flask(__name__)
2020-07-20 17:09:05 +02:00
app.config['MQTT_BROKER_URL'] = MQTT_HOSTNAME
2020-08-06 20:12:58 +02:00
MQTT_PORT = int(MQTT_PORT)
2020-07-20 17:09:05 +02:00
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
2020-08-06 20:12:58 +02:00
app.config['SQLALCHEMY_DATABASE_URI'] = \
2020-12-06 11:14:07 +01:00
f"postgresql://{POSTGRES_USERNAME}:{POSTGRES_PASSWORD}@{POSTGRES_HOSTNAME}:5432/{POSTGRES_DB}?sslmode=require"
2020-07-17 16:31:49 +02:00
api = Api(app)
db.init_app(app)
ma.init_app(app)
2020-07-20 17:09:05 +02:00
mqtt.init_app(app)
2020-12-06 11:14:07 +01:00
health = HealthCheck()
2020-07-17 16:31:49 +02:00
with app.app_context():
db.create_all()
2020-07-20 17:09:05 +02:00
@mqtt.on_log()
def handle_logging(client, userdata, level, buf):
logger.log(level, buf)
@mqtt.on_connect()
def handle_connect(client, userdata, flags, rc):
2020-07-26 17:45:19 +02:00
mqtt.subscribe(f"{MQTT_STATUS_TOPIC}/#")
@mqtt.on_topic(f"{config.MQTT_STATUS_TOPIC}/#")
def handle_status_message_proxy(*args, **kwargs):
"""
This proxy puts the call into a Flask context, so database operations can be performed
"""
with app.app_context():
handle_status_message(*args, **kwargs)
2020-09-20 02:14:41 +02:00
2020-07-26 17:45:19 +02:00
api.add_resource(AllDevicesResource, "/devices")
api.add_resource(AllDevicesOfflineResource, "/devices/offline")
api.add_resource(AllDevicesOnlineResource, "/devices/online")
api.add_resource(DeviceResource, "/devices/{deviceid}")
api.add_resource(DeviceOfflineResrouce, "/devices/{deviceid}/offline")
api.add_resource(DeviceOnlineResrouce, "/devices/{deviceid}/online")
api.add_resource(SensorResource, "/devices/{deviceid}/{sensorid}")
api.add_resource(SensorOfflineResource, "/devices/{deviceid}/{sensorid}/offline")
api.add_resource(SensorOnlineResource, "/devices/{deviceid}/{sensorid}/online")
2020-07-17 16:31:49 +02:00
2020-11-19 01:54:16 +01:00
health.add_check(health_database_status)
2020-12-06 11:14:07 +01:00
app.add_url_rule("/healthz", "healthcheck", view_func=lambda: health.run())