Onset: version control where nobody waits and nothing gets lost. The on command, its core and onset-server.
| 6 |
| """ |
| 7 | import http.server |
| 8 | import json |
| 9 | import sys |
| 10 |
| 11 | PORT, TOKEN = int(sys.argv[1]), sys.argv[2] |
| 12 | polls = {} |
| 13 |
| 14 |
| 15 | class Handler(http.server.BaseHTTPRequestHandler): |
| 16 | def log_message(self, *args): |
| 17 | pass |
| 18 |
| 19 | def reply(self, value): |
| 20 | body = json.dumps({"data": value}).encode() |
| 21 | self.send_response(200) |
| 22 | self.send_header("content-type", "application/json") |
| 23 | self.send_header("content-length", str(len(body))) |
| 24 | self.end_headers() |
| 25 | self.wfile.write(body) |
| 26 |
| 27 | def do_POST(self): |
| 28 | self.rfile.read(int(self.headers.get("content-length") or 0)) |
| 29 | self.reply({"challengeId": "chal1", "verifyUrl": f"http://127.0.0.1:{PORT}/verify/chal1", "expiresAt": ""}) |
| 30 |
| 31 | def do_GET(self): |
| 32 | n = polls.get(self.path, 0) + 1 |
| 33 | polls[self.path] = n |
| 34 | self.reply({"challengeId": "chal1", "status": "pending" if n < 2 else "approved", "token": TOKEN if n >= 2 else None}) |
| 35 |
| 36 |
| 37 | http.server.HTTPServer(("127.0.0.1", PORT), Handler).serve_forever() |
| 38 |