ocngx / docker-start.sh
tanbushi's picture
update
bb07043
Raw
History Blame
2.74 kB
#!/bin/bash
# Docker 启动脚本:同时启动 OpenResty 和 OpenCode
echo "🚀 Starting OpenResty + OpenCode Integration..."
# 检查环境变量
export GATEWAY_HOST=${GATEWAY_HOST:-127.0.0.1}
export GATEWAY_PORT=${GATEWAY_PORT:-57860}
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}"
# 启动 OpenCode 服务器在后台
echo "🤖 Starting OpenCode server on port ${GATEWAY_PORT}..."
opencode serve --port ${GATEWAY_PORT} --hostname ${GATEWAY_HOST} &
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"
else
echo "❌ OpenCode server failed to start"
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: admin/admin123"
echo "🌐 Nginx serving: http://localhost:7860"
echo "🔗 API Gateway: http://localhost:7860/opencode/"
else
echo "❌ OpenResty failed to start"
# 清理进程
kill $OPENCODE_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/opencode/"
echo " • OpenCode Docs: http://localhost:7860/opencode/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
# 保持容器运行,等待信号
wait