Launches web server to serve the client database via http using configurable port

This commit is contained in:
bj
2020-10-16 15:59:03 +02:00
parent 901b3ca960
commit a67b3ef441
5 changed files with 50 additions and 4 deletions
+9
View File
@@ -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,
+15
View File
@@ -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'
)