#!/bin/bash

# 后台启动脚本

cd "$(dirname "$0")"

# 创建必要目录
mkdir -p data uploads logs

# 停止已运行的进程
if [ -f server.pid ]; then
    PID=$(cat server.pid)
    if ps -p $PID > /dev/null 2>&1; then
        echo "停止已运行的进程 (PID: $PID)..."
        kill $PID 2>/dev/null
        sleep 1
    fi
    rm -f server.pid
fi

# 启动服务
echo "启动服务器..."
nohup node server.js > logs/server.log 2>&1 &
echo $! > server.pid

echo ""
echo "========================================="
echo "  服务器已启动"
echo "========================================="
echo ""
echo "访问地址:"
echo "  首页:   http://38.55.107.61:3000"
echo "  登录:   http://38.55.107.61:3000/login"
echo "  后台:   http://38.55.107.61:3000/admin"
echo ""
echo "默认账号: admin / admin123"
echo ""
echo "查看日志: tail -f logs/server.log"
echo "停止服务: ./stop.sh"
echo "========================================="
