Fixed HTTP backend

This commit is contained in:
Fabio Manganiello 2018-01-04 17:20:35 +01:00
parent a476de81a8
commit 39f986a88d

View file

@ -1,9 +1,11 @@
import logging import logging
import json import json
import time
from multiprocessing import Process from multiprocessing import Process
from flask import Flask, abort, jsonify, request from flask import Flask, abort, jsonify, request
from platypush.config import Config
from platypush.message import Message from platypush.message import Message
from platypush.message.request import Request from platypush.message.request import Request
@ -12,7 +14,7 @@ from .. import Backend
class HttpBackend(Backend): class HttpBackend(Backend):
""" Example interaction with the HTTP backend to make requests: """ Example interaction with the HTTP backend to make requests:
$ curl -XPOST \ $ curl -XPOST -d "token=your_configured_token" \
-d 'msg={"type":"request","target":"volta","action":"tts.say","args": {"phrase":"This is a test"}}' \ -d 'msg={"type":"request","target":"volta","action":"tts.say","args": {"phrase":"This is a test"}}' \
http://localhost:8008 """ http://localhost:8008 """
@ -20,44 +22,50 @@ class HttpBackend(Backend):
super().__init__(**kwargs) super().__init__(**kwargs)
self.port = port self.port = port
self.token = token self.token = token
self.server_proc = None
def send_message(self, msg): def send_message(self, msg):
raise NotImplementedError('Use cURL or any HTTP client to query the HTTP backend') logging.warning('Use cURL or any HTTP client to query the HTTP backend')
def _start_server(self): def stop(self):
def app_main(): logging.info('Received STOP event on HttpBackend')
if self.server_proc:
self.server_proc.terminate()
self.server_proc.join()
def run(self):
super().run()
app = Flask(__name__) app = Flask(__name__)
@app.route('/', methods=['POST']) @app.route('/', methods=['POST'])
def index(): def index():
args = { k:v for (k,v) in request.form.items() } args = { k:v for (k,v) in request.form.items() }
if self.token: if self.token and ('token' not in args or args['token'] != self.token): abort(401)
if 'token' not in args or args['token'] != self.token: if 'msg' not in args: abort(400)
abort(401)
if 'msg' not in args:
abort(400)
msg = Message.build(args['msg']) msg = Message.build(args['msg'])
logging.debug('Received message on HTTP backend: {}'.format(msg)) logging.info('Received message on the HTTP backend: {}'.format(msg))
if isinstance(msg, Request): if isinstance(msg, Request):
response = msg.execute(async=False) response = msg.execute(async=False)
logging.info('Processing response on the HTTP backend: {}'.format(msg))
return str(response) return str(response)
return jsonify({ 'status': 'ok' }) return jsonify({ 'status': 'ok' })
app.run(debug=True, host='0.0.0.0', port=self.port)
logging.info('Initialized HTTP backend on port {}'.format(self.port)) logging.info('Initialized HTTP backend on port {}'.format(self.port))
return app_main self.server_proc = Process(target=app.run, kwargs={
'debug':True, 'host':'0.0.0.0', 'port':self.port
})
time.sleep(1)
def run(self):
super().run()
self.server_proc = Process(target=self._start_server())
self.server_proc.start() self.server_proc.start()
self.server_proc.join() self.server_proc.join()