File size: 6,759 Bytes
5d4c685 17bc34e 5d4c685 17bc34e 5d4c685 17bc34e |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
#!/usr/bin/env python3
"""
Hugging Face Spaces WordPress 应用入口文件
这个文件是 Hugging Face Spaces 的标准入口点,
但实际的 WordPress 应用运行在 Docker 容器中。
该文件主要用于:
1. 提供应用信息
2. 健康检查
3. 重定向到 WordPress
"""
import os
import time
import subprocess
from flask import Flask, redirect, jsonify, render_template_string
app = Flask(__name__)
# 应用信息
APP_INFO = {
"name": "WordPress for Hugging Face Spaces",
"version": "1.0.0",
"description": "WordPress 单容器部署,使用 SQLite 数据库,包含自动清理功能",
"author": "Hugging Face Spaces WordPress Team",
"wordpress_url": "http://localhost:7860"
}
# HTML 模板
INDEX_TEMPLATE = """
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ app_info.name }}</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
margin: 0;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: white;
border-radius: 10px;
padding: 40px;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
text-align: center;
max-width: 600px;
}
h1 {
color: #333;
margin-bottom: 10px;
}
.version {
color: #666;
font-size: 14px;
margin-bottom: 20px;
}
.description {
color: #555;
line-height: 1.6;
margin-bottom: 30px;
}
.btn {
display: inline-block;
background: #667eea;
color: white;
padding: 12px 30px;
text-decoration: none;
border-radius: 5px;
font-weight: bold;
transition: background 0.3s;
margin: 10px;
}
.btn:hover {
background: #5a6fd8;
}
.status {
margin-top: 30px;
padding: 15px;
background: #f8f9fa;
border-radius: 5px;
border-left: 4px solid #28a745;
}
.features {
text-align: left;
margin: 20px 0;
}
.features ul {
list-style-type: none;
padding: 0;
}
.features li {
padding: 5px 0;
position: relative;
padding-left: 20px;
}
.features li:before {
content: "✓";
position: absolute;
left: 0;
color: #28a745;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h1>{{ app_info.name }}</h1>
<div class="version">版本 {{ app_info.version }}</div>
<div class="description">{{ app_info.description }}</div>
<div class="features">
<h3>主要特性:</h3>
<ul>
<li>单容器 Docker 部署</li>
<li>SQLite 轻量级数据库</li>
<li>自动文件清理 (保留1年)</li>
<li>性能优化配置</li>
<li>安全防护机制</li>
<li>实时监控面板</li>
</ul>
</div>
<a href="/wordpress" class="btn">进入 WordPress</a>
<a href="/health" class="btn">系统状态</a>
<div class="status">
<strong>状态:</strong> WordPress 正在运行中...<br>
<small>如果这是首次访问,WordPress 可能需要几分钟来初始化</small>
</div>
</div>
</body>
</html>
"""
@app.route('/')
def index():
"""主页 - 显示应用信息"""
return render_template_string(INDEX_TEMPLATE, app_info=APP_INFO)
@app.route('/wordpress')
def wordpress():
"""重定向到 WordPress"""
return redirect('http://localhost:7860', code=302)
@app.route('/health')
def health():
"""健康检查端点"""
try:
# 检查 WordPress 容器是否运行
result = subprocess.run(
['curl', '-f', '-s', 'http://localhost:7860'],
capture_output=True,
timeout=5
)
wordpress_status = "running" if result.returncode == 0 else "stopped"
# 获取系统信息
disk_usage = subprocess.run(
['df', '-h', '/'],
capture_output=True,
text=True
).stdout.split('\n')[1].split()[4] if subprocess.run(['df', '-h', '/'], capture_output=True).returncode == 0 else "unknown"
return jsonify({
"status": "healthy",
"timestamp": time.time(),
"services": {
"wordpress": wordpress_status,
"database": "sqlite",
"cleanup": "enabled"
},
"system": {
"disk_usage": disk_usage,
"uptime": time.time()
},
"app_info": APP_INFO
})
except Exception as e:
return jsonify({
"status": "error",
"error": str(e),
"timestamp": time.time()
}), 500
@app.route('/api/info')
def api_info():
"""API 信息端点"""
return jsonify(APP_INFO)
@app.route('/api/cleanup/status')
def cleanup_status():
"""清理状态 API"""
try:
# 读取清理日志
log_file = '/var/log/wordpress/cleanup.log'
if os.path.exists(log_file):
with open(log_file, 'r') as f:
lines = f.readlines()
recent_logs = lines[-10:] if len(lines) > 10 else lines
else:
recent_logs = ["暂无清理记录"]
return jsonify({
"status": "success",
"cleanup_enabled": True,
"retention_days": 365,
"recent_logs": [line.strip() for line in recent_logs],
"log_file": log_file
})
except Exception as e:
return jsonify({
"status": "error",
"error": str(e)
}), 500
if __name__ == '__main__':
# 在 Hugging Face Spaces 中,应用应该监听端口 7860
port = int(os.environ.get('PORT', 7860))
print(f"启动 {APP_INFO['name']} v{APP_INFO['version']}")
print(f"监听端口: {port}")
print(f"WordPress URL: {APP_INFO['wordpress_url']}")
app.run(
host='0.0.0.0',
port=port,
debug=False
) |