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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/bin/bash

# 调试信息
echo "===== Open WebUI with Cloudflare Tunnel ====="
echo "User: $(whoami)"
echo "Home: $HOME"
echo "Working dir: $(pwd)"
echo "=============================================="

# 创建简单的 HTML 页面
# 确保目录存在并有正确权限
mkdir -p /var/www/html
chmod 755 /var/www/html 2>/dev/null || echo "无法修改 /var/www/html 权限,将使用临时目录"

# 尝试创建 HTML 文件,如果失败则使用临时目录
if cat > /var/www/html/index.html 2>/dev/null <<'EOF'
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Open WebUI Service</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            margin: 0;
            padding: 0;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
        }
        .container {
            text-align: center;
            background: rgba(255, 255, 255, 0.1);
            padding: 3rem;
            border-radius: 20px;
            backdrop-filter: blur(10px);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            max-width: 500px;
        }
        h1 {
            font-size: 2.5rem;
            margin-bottom: 1rem;
            font-weight: 300;
        }
        .subtitle {
            font-size: 1.2rem;
            margin-bottom: 2rem;
            opacity: 0.9;
        }
        .status {
            background: rgba(76, 175, 80, 0.2);
            padding: 1rem;
            border-radius: 10px;
            border: 1px solid rgba(76, 175, 80, 0.3);
            margin-bottom: 2rem;
        }
        .info {
            background: rgba(255, 255, 255, 0.1);
            padding: 1.5rem;
            border-radius: 10px;
            text-align: left;
            font-size: 0.9rem;
            line-height: 1.6;
        }
        .emoji {
            font-size: 3rem;
            margin-bottom: 1rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="emoji">🤖</div>
        <h1>hello world</h1>
        <div class="subtitle">hello world</div>
        
        <div class="status">
            <strong>hello world</strong><br>
            hello world
        </div>
        
        <div class="info">
            <strong>hello world</strong><br>
            • hello world<br>
            • hello world<br>
            • hello world<br>
            • hello world<br><br>
            
            <strong>📝 Note:</strong><br>
            hello world
        </div>
    </div>
</body>
</html>
EOF
then
    echo "HTML 文件创建成功:/var/www/html/index.html"
    WEB_ROOT="/var/www/html"
else
    echo "无法写入 /var/www/html/index.html,使用临时目录..."
    mkdir -p /tmp/www
    cat > /tmp/www/index.html <<'EOF'
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Open WebUI Service</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            margin: 0;
            padding: 0;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
        }
        .container {
            text-align: center;
            background: rgba(255, 255, 255, 0.1);
            padding: 3rem;
            border-radius: 20px;
            backdrop-filter: blur(10px);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            max-width: 500px;
        }
        h1 {
            font-size: 2.5rem;
            margin-bottom: 1rem;
            font-weight: 300;
        }
        .subtitle {
            font-size: 1.2rem;
            margin-bottom: 2rem;
            opacity: 0.9;
        }
        .status {
            background: rgba(76, 175, 80, 0.2);
            padding: 1rem;
            border-radius: 10px;
            border: 1px solid rgba(76, 175, 80, 0.3);
            margin-bottom: 2rem;
        }
        .info {
            background: rgba(255, 255, 255, 0.1);
            padding: 1.5rem;
            border-radius: 10px;
            text-align: left;
            font-size: 0.9rem;
            line-height: 1.6;
        }
        .emoji {
            font-size: 3rem;
            margin-bottom: 1rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="emoji">🤖</div>
        <h1>Open WebUI Service</h1>
        <div class="subtitle">Secure AI Chat Interface</div>

        <div class="status">
            <strong>Service Status: Running</strong><br>
            Backend accessible via Cloudflare Tunnel
        </div>

        <div class="info">
            <strong>🔒 Security Features:</strong><br>
            • End-to-end encrypted tunnel<br>
            • Private backend deployment<br>
            • Automatic data backup<br>
            • No direct exposure<br><br>

            <strong>📝 Note:</strong><br>
            Access the actual Open WebUI through your Cloudflare Tunnel domain.
        </div>
    </div>
</body>
</html>
EOF
    WEB_ROOT="/tmp/www"
    echo "HTML 文件创建在临时目录:/tmp/www/index.html"
fi

# 验证 HTML 文件是否创建成功
if [ ! -f "$WEB_ROOT/index.html" ]; then
    echo "错误:无法创建 HTML 文件"
    exit 1
fi

# 配置 Nginx
cat > /tmp/nginx.conf <<EOF
worker_processes auto;
pid /tmp/nginx.pid;
error_log /tmp/nginx_error.log;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    
    access_log /tmp/nginx_access.log;
    
    client_body_temp_path /tmp/client_body;
    fastcgi_temp_path /tmp/fastcgi_temp;
    proxy_temp_path /tmp/proxy_temp;
    scgi_temp_path /tmp/scgi_temp;
    uwsgi_temp_path /tmp/uwsgi_temp;
    
    server {
        listen 7860;
        root $WEB_ROOT;
        index index.html;
        
        location / {
            try_files \$uri \$uri/ =404;
        }
    }
}
EOF

# 创建临时目录
mkdir -p /tmp/client_body /tmp/fastcgi_temp /tmp/proxy_temp /tmp/scgi_temp /tmp/uwsgi_temp

# 启动 Nginx(前端展示)
echo "Starting Nginx for frontend display..."
nginx -c /tmp/nginx.conf -g "daemon off;" &
NGINX_PID=$!

# 启动数据同步服务(后台)
echo "Starting data sync service..."
/sync_data.sh &
SYNC_PID=$!

# 等待一下让服务启动
sleep 3

# 启动 Open WebUI(后台,内部端口 8080)
echo "Starting Open WebUI service..."

# 运行环境检查
if [ -f "/check_openwebui_env.sh" ]; then
    echo "运行 Open WebUI 环境检查..."
    chmod +x /check_openwebui_env.sh
    /check_openwebui_env.sh
fi

# 设置 Open WebUI 必需的环境变量
export DATA_DIR="/app/backend/data"
export WEBUI_SECRET_KEY="${WEBUI_SECRET_KEY:-$(openssl rand -hex 32)}"
export WEBUI_JWT_SECRET_KEY="${WEBUI_JWT_SECRET_KEY:-$(openssl rand -hex 32)}"
export PORT=8080
export HOST=0.0.0.0
export ENV=production
export PYTHONPATH="/app/backend:$PYTHONPATH"

# 创建数据目录并设置权限
mkdir -p "$DATA_DIR"
chmod 777 "$DATA_DIR"

# 创建可写的工作目录
mkdir -p /tmp/openwebui
cd /tmp/openwebui

# 复制必要的文件到可写目录
if [ -d "/app/backend" ]; then
    cp -r /app/backend/* /tmp/openwebui/ 2>/dev/null || true
fi

echo "环境变量设置:"
echo "- DATA_DIR: $DATA_DIR"
echo "- PORT: $PORT"
echo "- HOST: $HOST"
echo "- ENV: $ENV"

# 查找正确的启动方式
if command -v open-webui >/dev/null 2>&1; then
    echo "使用 open-webui 命令启动"
    open-webui serve --host $HOST --port $PORT &
    WEBUI_PID=$!
elif [ -f "/app/backend/start.sh" ]; then
    echo "使用 /app/backend/start.sh 启动 Open WebUI"
    cd /app/backend
    chmod +x start.sh
    ./start.sh &
    WEBUI_PID=$!
elif [ -f "/app/start.sh" ]; then
    echo "使用 /app/start.sh 启动 Open WebUI"
    cd /app
    chmod +x start.sh
    ./start.sh &
    WEBUI_PID=$!
elif [ -f "/tmp/openwebui/main.py" ]; then
    echo "使用 Python 直接启动 Open WebUI"
    cd /tmp/openwebui
    python3 -m uvicorn main:app --host $HOST --port $PORT &
    WEBUI_PID=$!
else
    echo "错误:找不到 Open WebUI 启动方式"
    echo "尝试使用 uvicorn 启动..."
    cd /app/backend
    python3 -m uvicorn open_webui.main:app --host $HOST --port $PORT &
    WEBUI_PID=$!
fi

# 等待 Open WebUI 启动
echo "Waiting for Open WebUI to start..."
echo "Open WebUI PID: $WEBUI_PID"

# 等待更长时间并检查进程状态
for i in {1..30}; do
    if kill -0 $WEBUI_PID 2>/dev/null; then
        echo "Open WebUI 进程正在运行 (检查 $i/30)"
        if curl -s http://localhost:8080 > /dev/null; then
            echo "✅ Open WebUI 启动成功并响应请求"
            break
        fi
    else
        echo "❌ Open WebUI 进程已停止"
        break
    fi
    sleep 2
done

# 最终检查
if ! curl -s http://localhost:8080 > /dev/null; then
    echo "⚠️  WARNING: Open WebUI 可能未完全就绪,但继续运行其他服务..."
    echo "请检查 Open WebUI 日志以获取更多信息"
fi

# 启动 Cloudflare Tunnel(如果配置了 TUNNEL_TOKEN)
if [ -n "$TUNNEL_TOKEN" ]; then
    echo "Starting Cloudflare Tunnel..."
    cloudflared tunnel run --token $TUNNEL_TOKEN 2>&1 | tee /tmp/cloudflared.log &
    TUNNEL_PID=$!
    echo "Cloudflare Tunnel started with PID: $TUNNEL_PID"
else
    echo "TUNNEL_TOKEN not set, skipping Cloudflare Tunnel startup"
    echo "Please set TUNNEL_TOKEN environment variable to enable tunnel"
fi

# 服务状态监控和保活
echo "All services started. Monitoring service status..."
while true; do
    echo "=== Service Status Check $(date) ==="
    
    # 检查 Nginx
    if kill -0 $NGINX_PID 2>/dev/null; then
        echo "✅ Nginx (Frontend): Running"
    else
        echo "❌ Nginx (Frontend): Stopped"
    fi
    
    # 检查 Open WebUI
    if kill -0 $WEBUI_PID 2>/dev/null; then
        echo "✅ Open WebUI: Running"
    else
        echo "❌ Open WebUI: Stopped"
    fi
    
    # 检查数据同步
    if kill -0 $SYNC_PID 2>/dev/null; then
        echo "✅ Data Sync: Running"
    else
        echo "❌ Data Sync: Stopped"
    fi
    
    # 检查 Cloudflare Tunnel
    if [ -n "$TUNNEL_TOKEN" ] && [ -n "$TUNNEL_PID" ]; then
        if kill -0 $TUNNEL_PID 2>/dev/null; then
            echo "✅ Cloudflare Tunnel: Running"
        else
            echo "❌ Cloudflare Tunnel: Stopped"
        fi
    fi
    
    # 检查端口监听
    echo "Port Status:"
    echo "- Port 7860 (Frontend): $(ss -tulpn | grep :7860 | wc -l) listeners"
    echo "- Port 8080 (Open WebUI): $(ss -tulpn | grep :8080 | wc -l) listeners"
    
    echo "=================================="
    
    # 保活 ping
    curl -s http://localhost:7860 > /dev/null || echo "Frontend ping failed"
    curl -s http://localhost:8080 > /dev/null || echo "Open WebUI ping failed"
    
    sleep 30
done