mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-06 13:05:09 +00:00
feat: 新版webui
This commit is contained in:
@@ -10,6 +10,7 @@ import { ALLRouter } from '@webapi/router';
|
||||
import { cors } from '@webapi/middleware/cors';
|
||||
import { createUrl } from '@webapi/utils/url';
|
||||
import { sendSuccess } from '@webapi/utils/response';
|
||||
import { join } from 'node:path';
|
||||
|
||||
// 实例化Express
|
||||
const app = express();
|
||||
@@ -43,13 +44,18 @@ export async function InitWebUi(logger: LogWrapper, pathWrapper: NapCatPathWrapp
|
||||
|
||||
// ------------挂载路由------------
|
||||
// 挂载静态路由(前端),路径为 [/前缀]/webui
|
||||
app.use(config.prefix + '/webui', express.static(pathWrapper.staticPath));
|
||||
app.use('/webui', express.static(pathWrapper.staticPath));
|
||||
// 挂载API接口
|
||||
app.use(config.prefix + '/api', ALLRouter);
|
||||
app.use('/api', ALLRouter);
|
||||
// 所有剩下的请求都转到静态页面
|
||||
const indexFile = join(pathWrapper.staticPath, 'index.html');
|
||||
|
||||
app.all(/\/webui\/(.*)/, (_req, res) => {
|
||||
res.sendFile(indexFile);
|
||||
});
|
||||
|
||||
// 初始服务(先放个首页)
|
||||
// WebUI只在config.prefix所示路径上提供服务,可配合Nginx挂载到子目录中
|
||||
app.all(config.prefix + '/', (_req, res) => {
|
||||
app.all('/', (_req, res) => {
|
||||
sendSuccess(res, null, 'NapCat WebAPI is now running!');
|
||||
});
|
||||
// ------------路由挂载结束------------
|
||||
@@ -58,14 +64,13 @@ export async function InitWebUi(logger: LogWrapper, pathWrapper: NapCatPathWrapp
|
||||
app.listen(config.port, config.host, async () => {
|
||||
// 启动后打印出相关地址
|
||||
const port = config.port.toString(),
|
||||
searchParams = { token: config.token },
|
||||
path = `${config.prefix}/webui`;
|
||||
searchParams = { token: config.token };
|
||||
if (config.host !== '' && config.host !== '0.0.0.0') {
|
||||
logger.log(`[NapCat] [WebUi] WebUi User Panel Url: ${createUrl(config.host, port, path, searchParams)}`);
|
||||
logger.log(`[NapCat] [WebUi] WebUi User Panel Url: https://napcat.152710.xyz/web_login?back=http://${config.host}:${config.port}${config.prefix}&token=${config.token}`);
|
||||
logger.log(
|
||||
`[NapCat] [WebUi] WebUi User Panel Url: ${createUrl(config.host, port, '/webui', searchParams)}`
|
||||
);
|
||||
}
|
||||
logger.log(`[NapCat] [WebUi] WebUi Local Panel Url: ${createUrl('127.0.0.1', port, path, searchParams)}`);
|
||||
logger.log(`[NapCat] [WebUi] WebUi Local Panel Url: https://napcat.152710.xyz/web_login?back=http://127.0.0.1:${config.port}${config.prefix}&token=${config.token}`);
|
||||
logger.log(`[NapCat] [WebUi] WebUi Local Panel Url: ${createUrl('127.0.0.1', port, '/webui', searchParams)}`);
|
||||
});
|
||||
// ------------Over!------------
|
||||
}
|
||||
|
||||
@@ -78,7 +78,6 @@ export class WebUiConfigWrapper {
|
||||
const defaultconfig: WebUiConfigType = {
|
||||
host: '0.0.0.0',
|
||||
port: 6099,
|
||||
prefix: '',
|
||||
token: '', // 默认先填空,空密码无法登录
|
||||
loginRate: 3,
|
||||
};
|
||||
@@ -90,7 +89,12 @@ export class WebUiConfigWrapper {
|
||||
try {
|
||||
const configPath = resolve(webUiPathWrapper.configPath, './webui.json');
|
||||
|
||||
if (!await fs.access(configPath, constants.F_OK).then(() => true).catch(() => false)) {
|
||||
if (
|
||||
!(await fs
|
||||
.access(configPath, constants.F_OK)
|
||||
.then(() => true)
|
||||
.catch(() => false))
|
||||
) {
|
||||
await fs.writeFile(configPath, JSON.stringify(defaultconfig, null, 4));
|
||||
}
|
||||
|
||||
@@ -98,13 +102,15 @@ export class WebUiConfigWrapper {
|
||||
// 更新配置字段后新增字段可能会缺失,同步一下
|
||||
const parsedConfig = this.applyDefaults(JSON.parse(fileContent) as Partial<WebUiConfigType>, defaultconfig);
|
||||
|
||||
if (!parsedConfig.prefix.startsWith('/')) parsedConfig.prefix = '/' + parsedConfig.prefix;
|
||||
if (parsedConfig.prefix.endsWith('/')) parsedConfig.prefix = parsedConfig.prefix.slice(0, -1);
|
||||
// 配置已经被操作过了,还是回写一下吧,不然新配置不会出现在配置文件里
|
||||
if (await fs.access(configPath, constants.W_OK).then(() => true).catch(() => false)) {
|
||||
if (
|
||||
await fs
|
||||
.access(configPath, constants.W_OK)
|
||||
.then(() => true)
|
||||
.catch(() => false)
|
||||
) {
|
||||
await fs.writeFile(configPath, JSON.stringify(parsedConfig, null, 4));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
console.warn(`文件: ${configPath} 没有写入权限, 配置的更改部分可能会在重启后还原.`);
|
||||
}
|
||||
// 不希望回写的配置放后面
|
||||
@@ -143,7 +149,12 @@ export class WebUiConfigWrapper {
|
||||
}
|
||||
// 获取日志列表
|
||||
public static async GetLogsList(): Promise<string[]> {
|
||||
if (await fs.access(webUiPathWrapper.logsPath, constants.F_OK).then(() => true).catch(() => false)) {
|
||||
if (
|
||||
await fs
|
||||
.access(webUiPathWrapper.logsPath, constants.F_OK)
|
||||
.then(() => true)
|
||||
.catch(() => false)
|
||||
) {
|
||||
return (await fs.readdir(webUiPathWrapper.logsPath))
|
||||
.filter((file) => file.endsWith('.log'))
|
||||
.map((file) => file.replace('.log', ''));
|
||||
@@ -153,7 +164,12 @@ export class WebUiConfigWrapper {
|
||||
// 获取指定日志文件内容
|
||||
public static async GetLogContent(filename: string): Promise<string> {
|
||||
const logPath = resolve(webUiPathWrapper.logsPath, `${filename}.log`);
|
||||
if (await fs.access(logPath, constants.R_OK).then(() => true).catch(() => false)) {
|
||||
if (
|
||||
await fs
|
||||
.access(logPath, constants.R_OK)
|
||||
.then(() => true)
|
||||
.catch(() => false)
|
||||
) {
|
||||
return await fs.readFile(logPath, 'utf-8');
|
||||
}
|
||||
return '';
|
||||
|
||||
1
src/webui/src/types/config.d.ts
vendored
1
src/webui/src/types/config.d.ts
vendored
@@ -1,7 +1,6 @@
|
||||
interface WebUiConfigType {
|
||||
host: string;
|
||||
port: number;
|
||||
prefix: string;
|
||||
token: string;
|
||||
loginRate: number;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user