From 67b630401b3b3d98281c917308282c0071f1013e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 20 Jul 2018 14:41:53 +0200 Subject: [PATCH] =?UTF-8?q?Implement=20TFW=20controller=20after=20months?= =?UTF-8?q?=20of=20pain=20and=20suffering=20=C2=AF\=5F(=E3=83=84)=5F/?= =?UTF-8?q?=C2=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/opt/server.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 controller/opt/server.py diff --git a/controller/opt/server.py b/controller/opt/server.py new file mode 100644 index 0000000..82a3811 --- /dev/null +++ b/controller/opt/server.py @@ -0,0 +1,39 @@ +import os +import json + +from tornado.ioloop import IOLoop +from tornado.web import RequestHandler, Application + +from tfw import FSMAwareEventHandler + + +class ControllerPostHandler(RequestHandler): + # pylint: disable=abstract-method + def initialize(self, **kwargs): # pylint: disable=arguments-differ + self.controller = kwargs['controller'] + + def post(self, *args, **kwargs): + self.set_header('Content-Type', 'application/json') + self.write(json.dumps({ + 'solved': self.controller.in_accepted_state + })) + + +class ControllerEventHandler(FSMAwareEventHandler): + def handle_event(self, message): + pass + + +if __name__ == '__main__': + controller = ControllerEventHandler('controller') + application = Application([( + f'/{os.environ["SECRET"]}', + ControllerPostHandler, + {'controller': controller} + )]) + application.listen(os.environ['CONTROLLER_PORT']) + + try: + IOLoop.instance().start() + finally: + controller.cleanup()