| |
| """ |
| 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" |
| } |
|
|
| |
| 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: |
| |
| 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__': |
| |
| 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 |
| ) |