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