-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·189 lines (162 loc) · 4.91 KB
/
run.sh
File metadata and controls
executable file
·189 lines (162 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
#!/bin/bash
# CGI Quick Start - 启动脚本
# 快速启动开发服务器
# 设置脚本目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# 加载配置
if [[ -f "$SCRIPT_DIR/.env" ]]; then
source "$SCRIPT_DIR/.env"
fi
# 默认配置
PORT=${PORT:-8080}
HOST=${HOST:-127.0.0.1}
DEBUG=${DEBUG:-true}
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 打印带颜色的消息
print_msg() {
local color="$1"
local message="$2"
echo -e "${color}$message${NC}"
}
# 检查端口是否被占用
check_port() {
local port="$1"
if command -v lsof >/dev/null 2>&1; then
lsof -i ":$port" >/dev/null 2>&1
elif command -v netstat >/dev/null 2>&1; then
netstat -ln 2>/dev/null | grep ":$port " >/dev/null
elif command -v ss >/dev/null 2>&1; then
ss -ln 2>/dev/null | grep ":$port " >/dev/null
else
# 无法检查,假设端口可用
return 1
fi
}
# 查找可用端口
find_available_port() {
local start_port="$1"
local port="$start_port"
while check_port "$port"; do
port=$((port + 1))
if [[ $port -gt $((start_port + 100)) ]]; then
print_msg "$RED" "Error: No available port found in range $start_port-$((start_port + 100))"
exit 1
fi
done
echo "$port"
}
# 尝试打开浏览器
open_browser() {
local url="$1"
# 延迟打开浏览器
(sleep 2 && {
if command -v termux-open-url >/dev/null 2>&1; then
# Termux 环境
termux-open-url "$url"
elif command -v xdg-open >/dev/null 2>&1; then
# Linux
xdg-open "$url" >/dev/null 2>&1
elif command -v open >/dev/null 2>&1; then
# macOS
open "$url" >/dev/null 2>&1
elif command -v start >/dev/null 2>&1; then
# Windows
start "$url" >/dev/null 2>&1
fi
}) &
}
# 启动服务器
start_server() {
print_msg "$BLUE" "CGI Quick Start Server"
print_msg "$BLUE" "====================="
# 检查权限
if [[ ! -x "$SCRIPT_DIR/cgi-bin/main.cgi" ]]; then
print_msg "$YELLOW" "Setting execute permissions for CGI scripts..."
chmod +x "$SCRIPT_DIR/cgi-bin"/*.cgi "$SCRIPT_DIR/cgi-bin/modules"/*.sh 2>/dev/null
fi
# 检查端口可用性
if check_port "$PORT"; then
print_msg "$YELLOW" "Port $PORT is occupied, finding alternative..."
PORT=$(find_available_port "$PORT")
print_msg "$GREEN" "Using port: $PORT"
fi
# 创建临时配置文件
export PORT HOST DEBUG
print_msg "$GREEN" "Server starting on: http://$HOST:$PORT"
print_msg "$GREEN" "CGI endpoint: http://$HOST:$PORT/cgi-bin/main.cgi"
print_msg "$YELLOW" "Press Ctrl+C to stop the server"
echo ""
# 自动打开浏览器
open_browser "http://$HOST:$PORT"
# 启动服务器 (尝试多种方式)
cd "$SCRIPT_DIR"
if command -v python3 >/dev/null 2>&1; then
print_msg "$GREEN" "Using Python 3 HTTP server..."
python3 -m http.server "$PORT" --bind "$HOST" --cgi
elif command -v python2 >/dev/null 2>&1; then
print_msg "$GREEN" "Using Python 2 HTTP server..."
python2 -m SimpleHTTPServer "$PORT"
elif command -v python >/dev/null 2>&1; then
print_msg "$GREEN" "Using Python HTTP server..."
python -m http.server "$PORT" --bind "$HOST" --cgi 2>/dev/null || \
python -m SimpleHTTPServer "$PORT"
else
print_msg "$RED" "Error: Python not found. Please install Python to run the development server."
print_msg "$YELLOW" "Alternatively, configure your web server to serve this directory with CGI support."
exit 1
fi
}
# 显示帮助
show_help() {
cat << EOF
CGI Quick Start - Development Server
Usage: $0 [options]
Options:
-p, --port PORT Set server port (default: 8080)
-h, --host HOST Set server host (default: 127.0.0.1)
-d, --debug Enable debug mode
--help Show this help message
Environment:
PORT Server port
HOST Server host
DEBUG Debug mode (true/false)
Examples:
$0 # Start with default settings
$0 -p 3000 # Start on port 3000
$0 -h 0.0.0.0 # Listen on all interfaces
Configuration can also be set in .env file.
EOF
}
# 解析命令行参数
while [[ $# -gt 0 ]]; do
case $1 in
-p|--port)
PORT="$2"
shift 2
;;
-h|--host)
HOST="$2"
shift 2
;;
-d|--debug)
DEBUG=true
shift
;;
--help)
show_help
exit 0
;;
*)
print_msg "$RED" "Unknown option: $1"
show_help
exit 1
;;
esac
done
# 启动服务器
start_server