From 41352ce7e064b03d0e0bf93328386836086f40eb Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Fri, 22 Nov 2024 15:53:08 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20=E6=96=B0=E5=A2=9E=E5=90=AF?= =?UTF-8?q?=E5=8A=A8webui=20attach=E7=88=B6=E8=BF=9B=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/tools.yaml | 1 + index.js | 9 ++++++++ package.json | 5 +++++ server/app/r/api/config/route.js | 4 ++-- start-nextjs.js | 35 ++++++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 start-nextjs.js diff --git a/config/tools.yaml b/config/tools.yaml index b86c1f6..96ed8ee 100644 --- a/config/tools.yaml +++ b/config/tools.yaml @@ -1,3 +1,4 @@ +isOpenWebUI: false # 是否开启webui defaultPath: './data/rcmp4/' # 保存视频的位置 videoSizeLimit: 70 # 视频大小限制(单位MB),超过大小则转换成群文件上传 proxyAddr: '127.0.0.1' # 魔法地址 diff --git a/index.js b/index.js index d2f5c43..56c4d14 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,15 @@ import fs from "node:fs"; import path from "path"; import config from "./model/config.js"; +import { startNextJs } from "./start-nextjs.js"; if (!global.segment) { global.segment = (await import("oicq")).segment } // 加载版本号 const versionData = config.getConfig("version"); +// 加载是否使用WebUI +const isOpenWebUI = config.getConfig("tools").isOpenWebUI; // 加载名称 const packageJsonPath = path.join('./plugins', 'rconsole-plugin', 'package.json'); const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); @@ -35,4 +38,10 @@ for (let i in files) { } apps[name] = ret[i].value[Object.keys(ret[i].value)[0]]; } + +// 检查是否启动 webui +if (isOpenWebUI) { + startNextJs(); +} + export { apps }; diff --git a/package.json b/package.json index fb24f5a..a30c81a 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,11 @@ "name": "rconsole-plugin", "description": "R-Plugin", "type": "module", + "scripts": { + "dev": "cd server && next dev -p 4016", + "start": "cd server && next start -p 4016", + "build": "cd server && next build" + }, "dependencies": { "axios": "^1.3.4", "chart.js": "^4.4.6", diff --git a/server/app/r/api/config/route.js b/server/app/r/api/config/route.js index bfe1a14..2e6f69e 100644 --- a/server/app/r/api/config/route.js +++ b/server/app/r/api/config/route.js @@ -2,8 +2,9 @@ import fs from 'fs'; import yaml from 'js-yaml'; import path from 'path'; +const configPath = path.join(process.cwd(), "../", 'config', 'tools.yaml'); + export async function GET(req, res) { - const configPath = path.join(process.cwd(), "../", 'config', 'tools.yaml'); try { const yamlContent = await fs.promises.readFile(configPath, 'utf8'); const config = yaml.load(yamlContent); @@ -21,7 +22,6 @@ export async function GET(req, res) { } export async function POST(req, res) { - const configPath = path.join(process.cwd(), "../", 'config', 'tools.yaml'); try { const updates = await req.json(); diff --git a/start-nextjs.js b/start-nextjs.js new file mode 100644 index 0000000..f09359f --- /dev/null +++ b/start-nextjs.js @@ -0,0 +1,35 @@ +import { spawn } from 'child_process'; + +logger.info(`[R插件][Next.js监测], 父进程 PID: ${process.pid}`); + +let childProcess = null; + +// 启动子进程运行 Next.js +export const startNextJs = () => { + logger.info(logger.yellow('[R插件][Next.js监测],启动 Next.js 进程...')); + // 加载名称 + childProcess = spawn('pnpm', ['run', 'dev'], { + cwd: './plugins/rconsole-plugin', // 指定工作目录 + stdio: 'inherit' // 继承父进程的标准输入输出 + }); + + // 子进程异常退出时捕获信号 + childProcess.on('close', (code) => { + logger.error(`[R插件][Next.js监测],Next.js 进程发生异常 ${code}`); + childProcess = null; + }); +}; + +// 捕获父进程退出信号 +const cleanup = () => { + logger.info(logger.yellow('[R插件][Next.js监测] 父进程退出,终止子进程...')); + if (childProcess) { + childProcess.kill(); // 终止子进程 + } + process.exit(); +}; + +// 绑定父进程的退出信号 +process.on('SIGINT', cleanup); // Ctrl+C 信号 +process.on('SIGTERM', cleanup); // kill 命令信号 +process.on('exit', cleanup); // 正常退出