CatPtain commited on
Commit
9aed77b
·
verified ·
1 Parent(s): 76b2e43

Delete app.py

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