From a67b3ef441c8486f194270b887f9bf63e8e043fd Mon Sep 17 00:00:00 2001 From: Benjamin Dweck Date: Fri, 16 Oct 2020 15:59:03 +0200 Subject: [PATCH] Launches web server to serve the client database via http using configurable port --- requirements.txt | 9 ++++++++- setup.cfg | 11 ++++++++++- torch-sub.conf | 10 ++++++++-- torchsub/torch_sub.py | 9 +++++++++ torchsub/torch_sub_webserver.py | 15 +++++++++++++++ 5 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 torchsub/torch_sub_webserver.py diff --git a/requirements.txt b/requirements.txt index 08b9abf..5d4a376 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,9 @@ paho-mqtt~=1.5.1 -setuptools~=50.3.1 \ No newline at end of file +setuptools~=50.3.1 +pip~=20.2.3 +Jinja2~=2.11.2 +Werkzeug~=1.0.1 +itsdangerous~=1.1.0 +click~=7.1.2 +MarkupSafe~=1.1.1 +Flask~=1.1.2 \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 89728ef..f92a277 100644 --- a/setup.cfg +++ b/setup.cfg @@ -10,7 +10,16 @@ classifiers = [options] packages = find: -install_requires = paho-mqtt~=1.5.1 +install_requires = + paho-mqtt~=1.5.1 + setuptools~=50.3.1 + pip~=20.2.3 + Jinja2~=2.11.2 + Werkzeug~=1.0.1 + itsdangerous~=1.1.0 + click~=7.1.2 + MarkupSafe~=1.1.1 + Flask~=1.1.2 [options.entry_points] console_scripts = torch-sub=torchsub.torch_sub:main diff --git a/torch-sub.conf b/torch-sub.conf index e00ed0f..beb84e4 100644 --- a/torch-sub.conf +++ b/torch-sub.conf @@ -3,7 +3,13 @@ ############################################################################## [mqtt] -Topic=torch/+/wake #Required +#Topic is required +Topic=torch/+/wake [database] -Filename=torch_clients.json #Optional; Default: clients.json \ No newline at end of file +#Filename is optional and defaults to 'clients.json' +Filename=torch_clients.json + +[web] +#Port is optional and defaults to 8080 +Port=3434 \ No newline at end of file diff --git a/torchsub/torch_sub.py b/torchsub/torch_sub.py index 7235dac..48637e1 100644 --- a/torchsub/torch_sub.py +++ b/torchsub/torch_sub.py @@ -7,6 +7,8 @@ import threading import paho.mqtt.subscribe as mqtt +from torchsub import torch_sub_webserver + database_filename = "clients.json" database_lock = threading.Lock() @@ -44,6 +46,7 @@ class Config: self.topic = None self.tls = None self.database_file = database_filename + self.web_port = 8080 parser = self.do_cli_argument_parsing() (config_path, config_filename) = self.get_config_path(parser.parse_args()) print("Using torch configuration path: " + config_path) @@ -97,6 +100,8 @@ class Config: } if config.has_section('database'): self.database_file = config['database'].get('Filename', fallback=database_filename) + if config.has_section('web'): + self.web_port = config['web'].getint('Port', fallback=self.web_port) def main(): @@ -109,6 +114,10 @@ def main(): with open(database_filename, 'w') as database_blank: json.dump({}, database_blank) + threading.Thread(target=torch_sub_webserver.app.run, + args=("localhost", config.web_port), + daemon=True).start() + subscribe(config.broker_hostname, config.broker_port, config.topic, diff --git a/torchsub/torch_sub_webserver.py b/torchsub/torch_sub_webserver.py new file mode 100644 index 0000000..f19805d --- /dev/null +++ b/torchsub/torch_sub_webserver.py @@ -0,0 +1,15 @@ +from flask import Flask + +from torchsub import torch_sub + +app = Flask(__name__) + + +@app.route('/clients', methods=['GET']) +def clients(): + with open(torch_sub.database_filename, 'r') as database: + return app.response_class( + response=database.read(), + status=200, + mimetype='application/json' + )