chore: webui

This commit is contained in:
手瓜一十雪
2024-08-11 13:48:26 +08:00
parent 1046b68640
commit be4f59bc5e
13 changed files with 63 additions and 86 deletions

View File

@@ -13,8 +13,11 @@ const app = express();
* 无需参数。
* @returns {Promise<void>} 无返回值。
*/
export let WebUiConfig:WebUiConfigWrapper;
export let WebUiConfig: WebUiConfigWrapper;
export let webUiPathWrapper: NapCatPathWrapper;
export async function InitWebUi(logger: LogWrapper, pathWrapper: NapCatPathWrapper) {
webUiPathWrapper = pathWrapper;
WebUiConfig = new WebUiConfigWrapper();
let log = logger.log;
const config = await WebUiConfig.GetWebUIConfig();
@@ -31,7 +34,7 @@ export async function InitWebUi(logger: LogWrapper, pathWrapper: NapCatPathWrapp
});
});
// 配置静态文件服务,提供./static目录下的文件服务访问路径为/webui
app.use(config.prefix + '/webui', express.static(resolve(pathWrapper.staticPath, './static')));
app.use(config.prefix + '/webui', express.static(pathWrapper.staticPath));
//挂载API接口
app.use(config.prefix + '/api', ALLRouter);
app.listen(config.port, config.host, async () => {

View File

@@ -1,54 +0,0 @@
import { RequestHandler } from 'express';
import { resolve } from 'path';
import { readdir, stat } from 'fs/promises';
import { existsSync } from 'fs';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export const GetLogFileListHandler: RequestHandler = async (req, res) => {
try {
const LogsPath = resolve(__dirname, './logs/');
const LogFiles = await readdir(LogsPath);
res.json({
code: 0,
data: LogFiles,
});
} catch (error) {
res.json({ code: -1, msg: 'Failed to retrieve log file list.' });
}
};
export const GetLogFileHandler: RequestHandler = async (req, res) => {
const LogsPath = resolve(__dirname, './logs/');
const LogFile = req.query.file as string;
// if (!isValidFileName(LogFile)) {
// res.json({ code: -1, msg: 'LogFile is not safe' });
// return;
// }
const filePath = `${LogsPath}/${LogFile}`;
if (!existsSync(filePath)) {
res.status(404).json({ code: -1, msg: 'LogFile does not exist' });
return;
}
try {
const fileStats = await stat(filePath);
if (!fileStats.isFile()) {
res.json({ code: -1, msg: 'LogFile must be a file' });
return;
}
res.sendFile(filePath);
} catch (error) {
res.json({ code: -1, msg: 'Failed to send log file.' });
}
};
// export function isValidFileName(fileName: string): boolean {
// const invalidChars = /[\.\:\*\?\"\<\>\|\/\\]/;
// return !invalidChars.test(fileName);
// }

View File

@@ -1,13 +1,9 @@
import { RequestHandler } from 'express';
import { WebUiDataRuntime } from '../helper/Data';
import { existsSync, readFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { OB11Config } from '@/webui/ui/components/WebUiApiOB11Config';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import { resolve } from 'node:path';
import { webUiPathWrapper } from '@/webui';
const isEmpty = (data: any) =>
data === undefined || data === null || data === '';
@@ -21,14 +17,14 @@ export const OB11GetConfigHandler: RequestHandler = async (req, res) => {
return;
}
const uin = await WebUiDataRuntime.getQQLoginUin();
const configFilePath = resolve(__dirname, `./config/onebot11_${uin}.json`);
const configFilePath = resolve(webUiPathWrapper.configPath, `./onebot11_${uin}.json`);
//console.log(configFilePath);
let data: OB11Config;
try {
data = JSON.parse(
existsSync(configFilePath)
? readFileSync(configFilePath).toString()
: readFileSync(resolve(__dirname, './config/onebot11.json')).toString(),
: readFileSync(resolve(webUiPathWrapper.configPath, './onebot11.json')).toString(),
);
} catch (e) {
data = {} as OB11Config;
@@ -68,18 +64,6 @@ export const OB11SetConfigHandler: RequestHandler = async (req, res) => {
} catch (e) {
SetResult = false;
}
// let configFilePath = resolve(__dirname, `./config/onebot11_${await WebUiDataRuntime.getQQLoginUin()}.json`);
// try {
// JSON.parse(req.body.config)
// readFileSync(configFilePath);
// }
// catch (e) {
// //console.log(e);
// configFilePath = resolve(__dirname, `./config/onebot11.json`);
// }
// //console.log(configFilePath,JSON.parse(req.body.config));
// writeFileSync(configFilePath, JSON.stringify(JSON.parse(req.body.config), null, 4));
if (SetResult) {
res.send({
code: 0,

View File

@@ -1,3 +1,4 @@
import { webUiPathWrapper } from '@/webui';
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
import * as net from 'node:net';
import { resolve } from 'node:path';
@@ -96,7 +97,7 @@ export class WebUiConfigWrapper {
console.log('随机密码生成失败', e);
}
try {
const configPath = resolve(__dirname, './config/webui.json');
const configPath = resolve(webUiPathWrapper.configPath, './webui.json');
if (!existsSync(configPath)) {
writeFileSync(configPath, JSON.stringify(defaultconfig, null, 4));

View File

@@ -3,7 +3,7 @@ import { AuthHelper } from '../../src/helper/SignToken';
import { QQLoginRouter } from './QQLogin';
import { AuthRouter } from './auth';
import { OB11ConfigRouter } from './OB11Config';
import { WebUiConfig } from '../helper/config';
import { WebUiConfig } from '@/webui';
const router = Router();