feat: add token sync service and update docker-compose
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
FROM python:3.11-slim
|
||||
|
||||
# 安装 curl 用于与 Docker Socket 通信
|
||||
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
RUN pip install --no-cache-dir flask
|
||||
|
||||
WORKDIR /app
|
||||
COPY app.py .
|
||||
|
||||
EXPOSE 42000
|
||||
|
||||
CMD ["python", "app.py"]
|
||||
@@ -0,0 +1,36 @@
|
||||
from flask import Flask, request, jsonify
|
||||
import json
|
||||
import os
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# 配置信息
|
||||
TOKEN_FILE_PATH = "/data/.mi.token" # 容器内挂载路径
|
||||
SECRET_KEY = os.getenv("SYNC_SECRET_KEY", "my_private_key_123")
|
||||
CONTAINER_NAME = os.getenv("BOT_CONTAINER_NAME", "wework-xiaoai-bot")
|
||||
|
||||
@app.route('/sync', methods=['POST'])
|
||||
def sync_token():
|
||||
auth_key = request.headers.get('X-Auth-Key')
|
||||
if auth_key != SECRET_KEY:
|
||||
return jsonify({"msg": "Unauthorized"}), 401
|
||||
|
||||
try:
|
||||
token_data = request.json
|
||||
if not token_data:
|
||||
return jsonify({"msg": "No data received"}), 400
|
||||
|
||||
with open(TOKEN_FILE_PATH, 'w', encoding='utf-8') as f:
|
||||
json.dump(token_data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
# 通过 Docker Socket 重启机器人容器
|
||||
# 使用 curl 调用容器内挂载的 docker.sock
|
||||
os.system(f"curl --unix-socket /var/run/docker.sock -X POST http://localhost/containers/{CONTAINER_NAME}/restart")
|
||||
|
||||
return jsonify({"msg": "Success. Token updated and Bot restarted."})
|
||||
except Exception as e:
|
||||
return jsonify({"msg": str(e)}), 500
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 默认运行在 42000 端口
|
||||
app.run(host='0.0.0.0', port=42000)
|
||||
Reference in New Issue
Block a user