re: old webui

This commit is contained in:
手瓜一十雪 2024-08-11 13:10:31 +08:00
parent 39ed9dea01
commit aa12506221
5 changed files with 16 additions and 19 deletions

View File

@ -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 });
}

View File

@ -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<void>}
*/
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 () => {

View File

@ -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) => {

View File

@ -1,4 +1,5 @@
import { OB11Config } from '@/onebot11/config';
import { OB11Config } from "@/onebot/helper/config";
interface LoginRuntimeType {
LoginCurrentTime: number;

View File

@ -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<T>(obj: Partial<T>, defaults: T): T {
@ -141,4 +137,3 @@ class WebUiConfigWrapper {
}
}
export const WebUiConfig = new WebUiConfigWrapper();