From aa12506221a65781f4f608c9607404d6245831ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Sun, 11 Aug 2024 13:10:31 +0800 Subject: [PATCH] re: old webui --- src/common/framework/napcat.ts | 2 ++ src/webui/index.ts | 19 +++++++++---------- src/webui/src/api/Auth.ts | 2 +- src/webui/src/helper/Data.ts | 3 ++- src/webui/src/helper/config.ts | 9 ++------- 5 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/common/framework/napcat.ts b/src/common/framework/napcat.ts index ca08f1e3..86f0de45 100644 --- a/src/common/framework/napcat.ts +++ b/src/common/framework/napcat.ts @@ -9,12 +9,14 @@ export class NapCatPathWrapper { logsPath: string; configPath: string; cachePath: string; + staticPath: string; constructor(mainPath: string = dirname(fileURLToPath(import.meta.url))) { this.binaryPath = mainPath; this.logsPath = path.join(this.binaryPath, 'logs'); this.configPath = path.join(this.binaryPath, 'config'); this.cachePath = path.join(this.binaryPath, 'cache'); + this.staticPath = path.join(this.binaryPath, 'static'); if (fs.existsSync(this.logsPath)) { fs.mkdirSync(this.logsPath, { recursive: true }); } diff --git a/src/webui/index.ts b/src/webui/index.ts index 4bc6ad5d..c2b77d74 100644 --- a/src/webui/index.ts +++ b/src/webui/index.ts @@ -1,23 +1,22 @@ import express from 'express'; -import { dirname, resolve } from 'node:path'; +import { resolve } from 'node:path'; import { ALLRouter } from './src/router'; -import { WebUiConfig } from './src/helper/config'; -import { fileURLToPath } from 'node:url'; -import { log } from '@/common/utils/log'; +import { LogWrapper } from '@/common/utils/log'; +import { NapCatPathWrapper } from '@/common/framework/napcat'; +import { WebUiConfigWrapper } from './src/helper/config'; const app = express(); - -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); - /** * 初始化并启动WebUI服务。 * 该函数配置了Express服务器以支持JSON解析和静态文件服务,并监听6099端口。 * 无需参数。 * @returns {Promise} 无返回值。 */ -export async function InitWebUi() { +export let WebUiConfig:WebUiConfigWrapper; +export async function InitWebUi(logger: LogWrapper, pathWrapper: NapCatPathWrapper) { + WebUiConfig = new WebUiConfigWrapper(); + let log = logger.log; const config = await WebUiConfig.GetWebUIConfig(); if (config.port == 0) { log('[NapCat] [WebUi] Current WebUi is not run.'); @@ -32,7 +31,7 @@ export async function InitWebUi() { }); }); // 配置静态文件服务,提供./static目录下的文件服务,访问路径为/webui - app.use(config.prefix + '/webui', express.static(resolve(__dirname, './static'))); + app.use(config.prefix + '/webui', express.static(resolve(pathWrapper.staticPath, './static'))); //挂载API接口 app.use(config.prefix + '/api', ALLRouter); app.listen(config.port, config.host, async () => { diff --git a/src/webui/src/api/Auth.ts b/src/webui/src/api/Auth.ts index 5f9dbe30..366ded37 100644 --- a/src/webui/src/api/Auth.ts +++ b/src/webui/src/api/Auth.ts @@ -1,7 +1,7 @@ import { RequestHandler } from 'express'; import { AuthHelper } from '../helper/SignToken'; -import { WebUiConfig } from '../helper/config'; import { WebUiDataRuntime } from '../helper/Data'; +import { WebUiConfig } from '@/webui'; const isEmpty = (data: any) => data === undefined || data === null || data === ''; export const LoginHandler: RequestHandler = async (req, res) => { diff --git a/src/webui/src/helper/Data.ts b/src/webui/src/helper/Data.ts index 10594175..ae9d093e 100644 --- a/src/webui/src/helper/Data.ts +++ b/src/webui/src/helper/Data.ts @@ -1,4 +1,5 @@ -import { OB11Config } from '@/onebot11/config'; +import { OB11Config } from "@/onebot/helper/config"; + interface LoginRuntimeType { LoginCurrentTime: number; diff --git a/src/webui/src/helper/config.ts b/src/webui/src/helper/config.ts index 53ace027..d34bcc62 100644 --- a/src/webui/src/helper/config.ts +++ b/src/webui/src/helper/config.ts @@ -1,12 +1,8 @@ import { existsSync, readFileSync, writeFileSync } from 'node:fs'; -import { dirname, resolve } from 'node:path'; import * as net from 'node:net'; -import { fileURLToPath } from 'node:url'; +import { resolve } from 'node:path'; -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); - // 限制尝试端口的次数,避免死循环 const MAX_PORT_TRY = 100; @@ -76,7 +72,7 @@ export interface WebUiConfigType { } // 读取当前目录下名为 webui.json 的配置文件,如果不存在则创建初始化配置文件 -class WebUiConfigWrapper { +export class WebUiConfigWrapper { WebUiConfigData: WebUiConfigType | undefined = undefined; private applyDefaults(obj: Partial, defaults: T): T { @@ -141,4 +137,3 @@ class WebUiConfigWrapper { } } -export const WebUiConfig = new WebUiConfigWrapper();