JerryZhouYG commited on
Commit
afad3ef
·
verified ·
1 Parent(s): 102eadc

Create server.py

Browse files
Files changed (1) hide show
  1. server.py +76 -0
server.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template_string, jsonify
2
+ import subprocess
3
+ import threading
4
+
5
+ app = Flask(__name__)
6
+
7
+ # HTML 页面
8
+ HTML_PAGE = """
9
+ <!DOCTYPE html>
10
+ <html lang="zh">
11
+ <head>
12
+ <meta charset="UTF-8">
13
+ <title>手动同步</title>
14
+ <style>
15
+ body { font-family: Arial, sans-serif; margin: 20px; }
16
+ #logs { width: 100%; height: 300px; border: 1px solid #ccc; padding: 10px; overflow-y: scroll; background: #f4f4f4; }
17
+ button { padding: 10px 15px; font-size: 16px; }
18
+ </style>
19
+ <script>
20
+ function syncNow() {
21
+ let btn = document.getElementById("sync-btn");
22
+ btn.disabled = true;
23
+ fetch('/start_sync', { method: 'POST' })
24
+ .then(response => response.json())
25
+ .then(data => {
26
+ alert(data.message);
27
+ btn.disabled = false;
28
+ updateLogs();
29
+ });
30
+ }
31
+
32
+ function updateLogs() {
33
+ fetch('/logs')
34
+ .then(response => response.text())
35
+ .then(text => {
36
+ document.getElementById("logs").innerText = text;
37
+ });
38
+ }
39
+
40
+ setInterval(updateLogs, 2000);
41
+ </script>
42
+ </head>
43
+ <body>
44
+ <h2>手动同步管理</h2>
45
+ <button id="sync-btn" onclick="syncNow()">立即同步</button>
46
+ <pre id="logs">正在加载日志...</pre>
47
+ </body>
48
+ </html>
49
+ """
50
+
51
+ # Web 界面
52
+ @app.route('/sync')
53
+ def sync_page():
54
+ return render_template_string(HTML_PAGE)
55
+
56
+ # 启动同步
57
+ @app.route('/start_sync', methods=['POST'])
58
+ def start_sync():
59
+ def run_sync():
60
+ subprocess.run(["/bin/bash", "sync_now.sh"])
61
+
62
+ threading.Thread(target=run_sync, daemon=True).start()
63
+ return jsonify({"status": "success", "message": "手动同步开始,请等待..."})
64
+
65
+ # 获取日志内容
66
+ @app.route('/logs')
67
+ def get_logs():
68
+ try:
69
+ with open("/tmp/sync_log.txt", "r", encoding="utf-8") as f:
70
+ return f.read()
71
+ except FileNotFoundError:
72
+ return "暂无日志..."
73
+
74
+ # 启动 Flask
75
+ if __name__ == '__main__':
76
+ app.run(host="0.0.0.0", port=8000)