refactor: webui config

This commit is contained in:
手瓜一十雪 2024-05-20 16:56:34 +08:00
parent 7b09479cd2
commit 6980a9f3fc
3 changed files with 37 additions and 22 deletions

View File

@ -58,9 +58,6 @@ export abstract class HttpServerBase {
this.expressAPP.get('/', (req: Request, res: Response) => {
res.send(`${this.name}已启动`);
});
this.expressAPP.on('error', (err) => {
logError('HTTP服务启动失败', err.toString());
});
this.listen(port, host);
} catch (e: any) {
logError('HTTP服务启动失败', e.toString());
@ -117,6 +114,8 @@ export abstract class HttpServerBase {
this.server = this.expressAPP.listen(port, host, () => {
const info = `${this.name} started ${host}:${port}`;
log(info);
}).on('error', (err) => {
logError('HTTP服务启动失败', err.toString());
});
} catch (e: any) {
logError('HTTP服务启动失败, 请检查监听的ip地址和端口', e.stack.toString());

View File

@ -259,22 +259,44 @@ export class NapCatOnebot11 {
// throw new Error('Invalid configuration object');
// }
// 先设置
ob11Config.save(NewOb11);
const isHttpChanged = !isEqual(NewOb11.http.port, ob11Config.http.port) && NewOb11.http.enable;
const isHttpEnableChanged = !isEqual(NewOb11.http.enable, ob11Config.http.enable);
const isHttpPostChanged = !isEqual(NewOb11.http.postUrls, ob11Config.http.postUrls);
const isEnanleHttpPostChanged = !isEqual(NewOb11.http.enablePost, ob11Config.http.enablePost);
const isWsChanged = !isEqual(NewOb11.ws.port, ob11Config.ws.port);
const isEnableWsChanged = !isEqual(NewOb11.ws.enable, ob11Config.ws.enable);
const isEnableWsReverseChanged = !isEqual(NewOb11.reverseWs.enable, ob11Config.reverseWs.enable);
const isWsReverseUrlsChanged = !isEqual(NewOb11.reverseWs.urls, ob11Config.reverseWs.urls);
const isEnableHeartBeatChanged = !isEqual(NewOb11.heartInterval, ob11Config.heartInterval);
// http重启逻辑
if (isHttpChanged) {
ob11HTTPServer.restart(NewOb11.http.port, NewOb11.http.host);
}
if (!NewOb11.http.enable) {
ob11HTTPServer.stop();
} else {
ob11HTTPServer.start(NewOb11.http.port, NewOb11.http.host);
if (isHttpEnableChanged) {
if (NewOb11.http.enable) {
ob11HTTPServer.start(NewOb11.http.port, NewOb11.http.host);
} else {
ob11HTTPServer.stop();
}
}
// http post重启逻辑
// if(isHttpPostChanged){
// logDebug('http post urls changed, restart http server');
// }
// if(isEnanleHttpPostChanged){
// logDebug('http post enable changed, restart http server');
// }
// ws重启逻辑
if (isWsChanged) {
ob11WebsocketServer.restart(NewOb11.ws.port);
}
@ -287,6 +309,7 @@ export class NapCatOnebot11 {
}
}
// 反向ws重启逻辑
if (isEnableWsReverseChanged) {
if (NewOb11.reverseWs.enable) {
ob11ReverseWebsockets.start();
@ -294,16 +317,6 @@ export class NapCatOnebot11 {
ob11ReverseWebsockets.stop();
}
}
if (NewOb11.reverseWs.enable && isWsReverseUrlsChanged) {
logDebug('反向ws地址有变化, 重启反向ws服务');
ob11ReverseWebsockets.restart();
}
if (NewOb11.http.enableHeart) {
httpHeart.start();
} else if (!NewOb11.http.enableHeart) {
httpHeart.stop();
}
ob11Config.save(NewOb11);
} catch (e) {
logError('热重载配置失败', e);
}

View File

@ -35,10 +35,13 @@ setTimeout(() => {
}, 0);
class HTTPHeart{
class HTTPHeart {
intervalId: NodeJS.Timeout | null = null;
start(){
const { heartInterval, } = ob11Config;
start(NewHeartInterval: number | undefined = undefined) {
let { heartInterval } = ob11Config;
if (NewHeartInterval && !Number.isNaN(NewHeartInterval)) {
heartInterval = NewHeartInterval;
}
if (this.intervalId) {
clearInterval(this.intervalId);
}
@ -48,8 +51,8 @@ class HTTPHeart{
}, heartInterval);
}
stop(){
if (this.intervalId){
stop() {
if (this.intervalId) {
clearInterval(this.intervalId);
}
}