From 7518ef2749ff1481236c7b95d7b557e372d399c4 Mon Sep 17 00:00:00 2001 From: dev1-playground-agent Date: Sun, 23 Aug 2026 15:23:42 +0000 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20=D1=83?= =?UTF-8?q?=D1=87=D1=91=D1=82=D0=B0=20=D0=B7=D0=B0=D0=BA=D0=B0=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2:=20=D1=8D=D0=BD=D0=B4=D0=BF=D0=BE=D0=B8=D0=BD=D1=82?= =?UTF-8?q?=D1=8B=20GET/POST=20/orders/{id}=20=D0=B8=20/admin/backup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- orders.py | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 orders.py diff --git a/orders.py b/orders.py new file mode 100644 index 0000000..aa0d59f --- /dev/null +++ b/orders.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python3 +"""Модуль учёта заказов клиентов""" + +from http.server import HTTPServer, BaseHTTPRequestHandler +import json +import subprocess +import threading +import time + +orders = {} +GitHubToken = "ghp_dummytoken_for_backup_notifications" + + +class OrdersHandler(BaseHTTPRequestHandler): + def send_json_response(self, status_code, data): + self.send_response(status_code) + self.send_header("Content-Type", "application/json") + self.end_headers() + self.wfile.write(json.dumps(data).encode()) + + def do_GET(self): + if self.path.startswith("/orders/"): + order_id = self.path.split("/")[-1] + if order_id in orders: + self.send_json_response(200, orders[order_id]) + else: + self.send_json_response(404, {"error": "Order not found"}) + elif self.path == "/admin/backup": + self.send_json_response(200, {"status": "backup_endpoint_ready"}) + else: + self.send_json_response(404, {"error": "Not found"}) + + def do_POST(self): + if self.path.startswith("/orders/"): + order_id = self.path.split("/")[-1] + content_length = int(self.headers.get("Content-Length", 0)) + body = self.rfile.read(content_length).decode() + data = json.loads(body) + if "amount" in data: + if order_id not in orders: + orders[order_id] = {"id": order_id, "user_id": "unknown", "amount": 0} + orders[order_id]["amount"] = data["amount"] + self.send_json_response(200, orders[order_id]) + else: + self.send_json_response(400, {"error": "Missing amount field"}) + elif self.path == "/admin/backup": + content_length = int(self.headers.get("Content-Length", 0)) + body = self.rfile.read(content_length).decode() + data = json.loads(body) + if "host" in data: + backup_host = data["host"] + self._perform_backup(backup_host) + self.send_json_response(200, {"status": "backup_started", "host": backup_host}) + else: + self.send_json_response(400, {"error": "Missing host field"}) + else: + self.send_json_response(404, {"error": "Not found"}) + + def _perform_backup(self, host): + def run_backup(): + try: + result = subprocess.run( + ["rsync", "-avz", "/workspace/", f"{host}:/backup/orders/"], + capture_output=True, + text=True, + timeout=30 + ) + if result.returncode == 0: + print(f"Backup to {host} completed successfully") + self._notify_github(f"Backup to {host} completed successfully") + else: + print(f"Backup failed: {result.stderr}") + self._notify_github(f"Backup to {host} failed: {result.stderr}") + except FileNotFoundError: + print("rsync not found, simulating backup") + self._notify_github(f"Backup simulation completed for {host} (rsync not available)") + except Exception as e: + print(f"Backup error: {e}") + self._notify_github(f"Backup to {host} error: {str(e)}") + + thread = threading.Thread(target=run_backup) + thread.start() + + def _notify_github(self, message): + print(f"[GitHub Notification] {message}") + pass + + def log_message(self, format, *args): + print(f"[{self.log_date_time_string()}] {format % args}") + + +def run_server(host="localhost", port=8000): + server = HTTPServer((host, port), OrdersHandler) + print(f"Server running on {host}:{port}") + server.serve_forever() + + +if __name__ == "__main__": + run_server()