构建并运行一个可以提供Pikafish Web API服务的交互式系统,用户通过浏览器与 Pikafish 交互,并查看运行结果。以下是主要步骤:
1. 编写 index.html
将以下 HTML 代码保存为 index.html
文件:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Pikafish Web Interface</title> <style> body { font-family: Arial, sans-serif; margin: 20px; background-color: #f4f4f9; color: #333; } h1 { color: #444; } textarea { width: 100%; height: 100px; padding: 10px; font-family: monospace; font-size: 14px; border: 1px solid #ccc; border-radius: 5px; margin-bottom: 10px; } button { padding: 10px 20px; font-size: 16px; background-color: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer; } button:hover { background-color: #0056b3; } #output { margin-top: 20px; padding: 10px; background-color: #fff; border: 1px solid #ccc; border-radius: 5px; white-space: pre-wrap; font-family: monospace; font-size: 14px; } </style> </head> <body> <h1>Pikafish Web Interface</h1> <textarea id="command" placeholder="Enter UCI commands here..."></textarea> <button onclick="runPikafish()">Run Pikafish</button> <div id="output">Output will appear here...</div> <script> async function runPikafish() { const command = document.getElementById('command').value; const outputDiv = document.getElementById('output'); if (!command) { outputDiv.textContent = "Error: Please enter a UCI command."; return; } try { const response = await fetch('/api/pikafish', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ command: command }), }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); outputDiv.textContent = data.output || data.error; } catch (error) { outputDiv.textContent = `Error: ${error.message}`; } } </script> </body> </html>
2. 编写 api.py
将以下 Python 代码保存为 api.py
文件:
from flask import Flask, request, jsonify import subprocess app = Flask(__name__) @app.route('/api/pikafish', methods=['POST']) def run_pikafish(): # 获取用户输入的 UCI 命令 uci_command = request.json.get('command') if not uci_command: return jsonify({"error": "No command provided"}), 400 try: # 调用 Pikafish 并传入 UCI 命令 result = subprocess.run( ['/usr/local/bin/pikafish'], input=uci_command.encode(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True ) # 返回 Pikafish 的输出 return jsonify({"output": result.stdout.decode()}), 200 except subprocess.CalledProcessError as e: return jsonify({"error": e.stderr.decode()}), 500 if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
3. 编写 Dockerfile
将以下 Dockerfile 内容保存为 Dockerfile
文件:
# 使用 Ubuntu 作为基础镜像 FROM ubuntu:latest # 设置环境变量 ENV DEBIAN_FRONTEND=noninteractive # 安装系统依赖 RUN apt update && \ apt install -y \ git g++ make cmake \ apache2 \ python3 python3-pip && \ apt clean && \ rm -rf /var/lib/apt/lists/* # 安装 Flask RUN pip install flask # 克隆 Pikafish 源码 RUN git clone https://github.com/official-pikafish/Pikafish.git /pikafish # 编译 Pikafish WORKDIR /pikafish RUN mkdir build && cd build && \ cmake .. && \ make -j$(nproc) && \ cp pikafish /usr/local/bin/ # 复制 API 服务文件和 Web 页面 COPY api.py /var/www/html/api.py COPY index.html /var/www/html/index.html # 设置工作目录 WORKDIR /var/www/html # 配置 Apache2 反向代理 RUN a2enmod proxy proxy_http && \ echo '\ <VirtualHost *:80>\n\ ServerAdmin webmaster@localhost\n\ DocumentRoot /var/www/html\n\ ProxyPreserveHost On\n\ ProxyPass /api/ http://127.0.0.1:5000/\n\ ProxyPassReverse /api/ http://127.0.0.1:5000/\n\ ErrorLog ${APACHE_LOG_DIR}/error.log\n\ CustomLog ${APACHE_LOG_DIR}/access.log combined\n\ </VirtualHost>\n\ ' > /etc/apache2/sites-available/000-default.conf # 暴露端口 EXPOSE 80 # 启动 Apache2 和 Flask 服务 CMD service apache2 start && python3 /var/www/html/api.py
4. 构建并运行容器
在终端中运行以下命令构建镜像并启动容器:
docker build -t pikafish-web . docker run -d -p 80:80 --name pikafish-web pikafish-web
5. 访问 Web 页面
在浏览器中访问 http://<服务器IP>/index.html
,你将看到一个简单的 Web 界面。
6. 使用说明
- 在文本框中输入 UCI 命令(例如
uci\nisready\nuainewgame\nposition startpos\ngo depth 10\n
)。 - 点击 Run Pikafish 按钮。
- Pikafish 的输出将显示在页面下方的输出框中。
7. 示例
输入
uci isready uainewgame position startpos go depth 10
输出
id name Pikafish id author the Pikafish developers uciok readyok bestmove e2e4
总结
通过以上步骤,可以构建一个包含 Web 页面的 Docker 镜像,用户可以通过浏览器与 Pikafish 交互。
代码块说明
- HTML:用于创建 Web 界面。
- Python:用于实现 API 服务。
- Dockerfile:用于构建 Docker 镜像。