diff options
author | Jonathan Weth <git@jonathanweth.de> | 2020-07-27 11:25:42 +0200 |
---|---|---|
committer | Jonathan Weth <git@jonathanweth.de> | 2020-07-27 11:25:42 +0200 |
commit | 7c8f259f9b7523f5fc79d22c0eeab5226b3e1076 (patch) | |
tree | 8f22c458ef4e48c3da9ea5fa5c53c3aff4dfd71d /trigger.py | |
parent | 4db238a1395b33e76711d310f92090f803af41d4 (diff) | |
download | RWA.Support.SessionService-7c8f259f9b7523f5fc79d22c0eeab5226b3e1076.tar.gz RWA.Support.SessionService-7c8f259f9b7523f5fc79d22c0eeab5226b3e1076.tar.bz2 RWA.Support.SessionService-7c8f259f9b7523f5fc79d22c0eeab5226b3e1076.zip |
Create global trigger server and handle SIGINT correctly
Diffstat (limited to 'trigger.py')
-rw-r--r-- | trigger.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/trigger.py b/trigger.py new file mode 100644 index 0000000..a7fb66c --- /dev/null +++ b/trigger.py @@ -0,0 +1,36 @@ +import threading +from typing import Any, Callable +from wsgiref.simple_server import make_server + +import port_for +from flask import Flask, abort, request + + +class TriggerServerThread(threading.Thread): + """Simple Flask server (wrapped as thread) for triggering actions on sesssions.""" + + def __init__(self, trigger_method: Callable[[str], Any]): + super().__init__() + self.port = port_for.select_random() + + app = Flask(__name__) + + @app.route("/", methods=["POST"]) + def trigger(): + json = request.json + token = json.get("token", "") + r = trigger_method(token) + if r: + return "Successful triggered" + else: + return abort(403) + + self.srv = make_server("127.0.0.1", self.port, app) + self.ctx = app.app_context() + self.ctx.push() + + def run(self): + self.srv.serve_forever() + + def shutdown(self): + self.srv.shutdown() |