| #!/bin/bash |
|
|
| |
|
|
| echo "🚀 Starting OpenResty + OpenCode Integration..." |
|
|
| |
| export GATEWAY_HOST=${GATEWAY_HOST:-127.0.0.1} |
| export GATEWAY_PORT=${GATEWAY_PORT:-3000} |
|
|
| |
| 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_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}" |
|
|
| |
| echo "⏰ Starting system cron daemon..." |
| service cron start |
| CRON_PID=$! |
|
|
| |
| 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}" |
| |
| opencode web --port ${GATEWAY_PORT} --hostname ${GATEWAY_HOST} --cors "*" & |
| OPENCODE_PID=$! |
|
|
| |
| echo "⏳ Waiting for OpenCode to start..." |
| sleep 5 |
|
|
| |
| 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 |
|
|
| |
| echo "🌐 Starting OpenResty with nginx on port 7860..." |
| exec /usr/local/openresty/bin/openresty -g "daemon off;" & |
| OPENRESTY_PID=$! |
|
|
| |
| sleep 3 |
|
|
| |
| if curl -s http://localhost:7860/health > /dev/null; then |
| echo "✅ OpenResty started successfully" |
| |
| 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 "" |
| 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 |