Files
YG_FT_Platform/web/start-http-server.sh

136 lines
3.8 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
echo "🚀 启动 HTTP 服务器"
echo "===================="
echo ""
# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
echo "📂 当前目录: $SCRIPT_DIR"
echo ""
# 检测操作系统类型
echo "🔍 检测操作系统..."
OSTYPE_LOWER=$(echo "$OSTYPE" | tr '[:upper:]' '[:lower:]')
# 检测是否在 WSL 中
IS_WSL=0
if [[ -n "$WSLENV" ]] || grep -qi microsoft /proc/version 2>/dev/null; then
IS_WSL=1
OS_TYPE="WSL (Windows Subsystem for Linux)"
echo "📟 检测到 WSL 环境"
elif [[ "$OSTYPE_LOWER" == "linux-gnu"* ]]; then
OS_TYPE="Linux"
echo "🐧 检测到 Linux 环境"
elif [[ "$OSTYPE_LOWER" == "darwin"* ]]; then
OS_TYPE="macOS"
echo "🍎 检测到 macOS 环境"
elif [[ "$OSTYPE_LOWER" == "msys" ]] || [[ "$OSTYPE_LOWER" == "cygwin" ]]; then
OS_TYPE="Windows (MSYS/Cygwin)"
echo "🪟 检测到 Windows (MSYS/Cygwin) 环境"
else
OS_TYPE="Unknown"
echo "⚠️ 未识别的操作系统: $OSTYPE"
fi
echo "✅ 操作系统类型: $OS_TYPE"
echo ""
# 根据操作系统选择 Python 命令
PYTHON_CMD=""
PYTHON_VERSION=""
if command -v python3 &> /dev/null; then
PYTHON_CMD="python3"
PYTHON_VERSION=$(python3 --version 2>&1)
elif command -v python &> /dev/null; then
PYTHON_CMD="python"
PYTHON_VERSION=$(python --version 2>&1)
else
echo "❌ 错误: 未找到 Python"
echo ""
echo "💡 解决方案:"
echo " 1. 安装 Python 3.x"
echo " 2. 确保 python 或 python3 命令可用"
echo " 3. Windows 用户可以使用: start.bat"
echo ""
exit 1
fi
echo "✅ 使用 Python 命令: $PYTHON_CMD"
echo "✅ Python 版本: $PYTHON_VERSION"
echo ""
# 获取本机IP地址
SERVER_IP=""
case "$OS_TYPE" in
"Linux"|"macOS")
# Linux/macOS 使用 hostname -I
if command -v hostname &> /dev/null; then
SERVER_IP=$(hostname -I 2>/dev/null | awk '{print $1}' | head -n1)
if [ -n "$SERVER_IP" ] && [ "$SERVER_IP" != "localhost" ]; then
echo "🌐 检测到 IP 地址: $SERVER_IP"
else
echo "🌐 未检测到有效 IP将使用 localhost"
SERVER_IP="localhost"
fi
fi
;;
"WSL (Windows Subsystem for Linux)")
echo "💡 WSL 环境提示:"
echo " - 如果无法访问 localhost请检查 WSL 网络配置"
echo " - 建议在 Windows 浏览器中访问服务"
echo ""
echo "🌐 建议使用: http://localhost:8000 访问"
SERVER_IP="localhost"
;;
"Windows (MSYS/Cygwin)"|*)
# Windows 下使用 localhost 更可靠
echo "🌐 Windows 环境使用 localhost 访问"
SERVER_IP="localhost"
;;
esac
if [ -z "$SERVER_IP" ]; then
SERVER_IP="localhost"
fi
echo ""
echo "📱 访问地址:"
echo " 主页: http://$SERVER_IP:8000/pages/main.html"
echo " 登录: http://$SERVER_IP:8000/pages/login.html"
echo ""
echo "⚠️ 服务器将在端口 8000 启动"
echo "按 Ctrl+C 停止服务器"
echo ""
# 启动 HTTP 服务器
echo "🔧 启动中..."
echo ""
# WSL 环境特殊处理
if [ $IS_WSL -eq 1 ]; then
echo "💡 WSL 启动提示:"
echo " - 服务器在 WSL 中启动"
echo " - 如果 Windows 浏览器无法访问,可能需要配置 WSL 网络"
echo " - 或使用 Windows 的 Python 启动: python -m http.server 8000"
echo ""
fi
# 启动服务器
$PYTHON_CMD -m http.server 8000
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
echo ""
echo "❌ 服务器启动失败 (退出码: $EXIT_CODE)"
echo ""
echo "💡 故障排除:"
echo " 1. 检查端口 8000 是否被占用"
echo " 2. 确认 Python 3 已正确安装"
echo " 3. 尝试使用其他端口: $PYTHON_CMD -m http.server 9000"
echo ""
fi