ocngx / docker-start.sh
tanbushi's picture
迁移到 Linux 系统 cron,提升可靠性和性能
5144611
#!/bin/bash
# Docker 启动脚本:同时启动 OpenResty 和 OpenCode
echo "🚀 Starting OpenResty + OpenCode Integration..."
# 检查环境变量
export GATEWAY_HOST=${GATEWAY_HOST:-127.0.0.1}
export GATEWAY_PORT=${GATEWAY_PORT:-3000}
# 默认用户名:admin 默认密码:admin123
export USERNAME=${USERNAME:-admin}
export PASSWORD=${PASSWORD:-admin123}
echo "📍 OpenCode will listen on: ${GATEWAY_HOST}:${GATEWAY_PORT}"
echo "🌐 Nginx will listen on: 0.0.0.0:7860"
# 动态生成 .htpasswd 文件
HTPASSWD_FILE="/usr/local/openresty/nginx/conf/.htpasswd"
echo "🔐 Generating .htpasswd file with user: ${USERNAME}"
echo "${USERNAME}:$(openssl passwd -apr1 ${PASSWORD})" > ${HTPASSWD_FILE}
echo "✅ .htpasswd file generated at ${HTPASSWD_FILE}"
# 启动系统 Cron 服务
echo "⏰ Starting system cron daemon..."
service cron start
CRON_PID=$!
# 启动 OpenCode Web服务器在后台
echo "🤖 Starting OpenCode web server on port ${GATEWAY_PORT}..."
# 确保在正确的目录中启动
cd /app
# 设置环境变量
export OPENCODE_PUBLIC_URL="https://airsltd-ocngx.hf.space"
export PUBLIC_URL="https://airsltd-ocngx.hf.space"
export BASE_URL="https://airsltd-ocngx.hf.space"
echo "🔧 Setting OPENCODE_PUBLIC_URL: ${OPENCODE_PUBLIC_URL}"
# 添加CORS支持,允许所有来源(在代理环境中)
opencode web --port ${GATEWAY_PORT} --hostname ${GATEWAY_HOST} --cors "*" &
OPENCODE_PID=$!
# 等待 OpenCode 启动
echo "⏳ Waiting for OpenCode to start..."
sleep 5
# 检查 OpenCode 是否正常运行
if curl -s http://${GATEWAY_HOST}:${GATEWAY_PORT}/global/health > /dev/null; then
echo "✅ OpenCode server started successfully"
echo "📖 OpenCode API docs: http://${GATEWAY_HOST}:${GATEWAY_PORT}/doc"
echo "🔍 Checking available endpoints..."
echo "Root path response:"
curl -s http://${GATEWAY_HOST}:${GATEWAY_PORT}/ | head -5
echo ""
echo "Available paths:"
curl -s http://${GATEWAY_HOST}:${GATEWAY_PORT}/global/health
else
echo "❌ OpenCode server failed to start"
# 清理进程
kill $OPENCODE_PID $CRON_PID 2>/dev/null
exit 1
fi
# 启动 OpenResty
echo "🌐 Starting OpenResty with nginx on port 7860..."
exec /usr/local/openresty/bin/openresty -g "daemon off;" &
OPENRESTY_PID=$!
# 等待 OpenResty 启动
sleep 3
# 检查 OpenResty 是否正常运行
if curl -s http://localhost:7860/health > /dev/null; then
echo "✅ OpenResty started successfully"
# echo "🔐 Basic Auth enabled: tbs/tbs123"
echo "🌐 Nginx serving: http://localhost:7860"
echo "🔗 API Gateway: http://localhost:7860/"
else
echo "❌ OpenResty failed to start"
# 清理进程
kill $OPENCODE_PID $CRON_PID 2>/dev/null
exit 1
fi
echo ""
echo "🎉 Integration Complete!"
echo ""
echo "📋 Available Endpoints:"
echo " • Main Site: http://localhost:7860/"
echo " • Health Check: http://localhost:7860/health"
echo " • OpenCode API: http://localhost:7860/"
echo " • OpenCode Docs: http://localhost:7860/doc"
echo " • API Gateway Health: http://localhost:7860/gateway/health"
echo ""
# echo "🔐 Authentication: ${USERNAME}/${PASSWORD}"
echo ""
echo "🤖 OpenCode Status:"
if curl -s http://${GATEWAY_HOST}:${GATEWAY_PORT}/global/health > /dev/null; then
echo " ✅ Server: healthy"
echo " ✅ Version: $(curl -s http://${GATEWAY_HOST}:${GATEWAY_PORT}/global/health | grep -o '"version":"[^"]*' | cut -d'"' -f4)"
else
echo " ❌ Server: unhealthy"
fi
# 添加进程清理函数
cleanup() {
echo "🧹 Cleaning up processes..."
kill $OPENCODE_PID $OPENRESTY_PID $CRON_PID 2>/dev/null
service cron stop 2>/dev/null
echo "✅ Cleanup completed"
exit 0
}
# 捕获退出信号
trap cleanup SIGTERM SIGINT
# 保持容器运行,等待信号
wait