From 9afc0f66670f843496a39a1071c195f513162aa7 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 23:59:58 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E9=89=B4=E6=9D=83=E5=92=8C=E5=9B=9E?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/onebot/network/passive-http.ts | 40 ++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/onebot/network/passive-http.ts b/src/onebot/network/passive-http.ts index d554fb2c..7d8f9038 100644 --- a/src/onebot/network/passive-http.ts +++ b/src/onebot/network/passive-http.ts @@ -4,6 +4,7 @@ import express, { Express, Request, Response } from 'express'; import http from 'http'; import { NapCatCore } from '@/core'; import { NapCatOneBot11Adapter } from '../main'; +import { OB11Response } from '../action/OB11Response'; export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter { token: string; @@ -35,14 +36,19 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter { } open() { - if (this.isOpen) { - this.coreContext.context.logger.logError('Cannot open a closed HTTP server'); - return; - } - if (!this.isOpen) { - this.initializeServer(); - this.isOpen = true; + try { + if (this.isOpen) { + this.coreContext.context.logger.logError('Cannot open a closed HTTP server'); + return; + } + if (!this.isOpen) { + this.initializeServer(); + this.isOpen = true; + } + } catch (e) { + this.coreContext.context.logger.logError(`[OneBot] [HTTP Server Adapter] Boot Error: ${e}`); } + } async close() { @@ -61,7 +67,7 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter { this.app.use('/', (req, res) => this.handleRequest(req, res)); this.server.listen(this.port, () => { - this.coreContext.context.logger.log(`[OneBot] [HTTP Adapter] Start On Port ${this.port}`); + this.coreContext.context.logger.log(`[OneBot] [HTTP Server Adapter] Start On Port ${this.port}`); }); } @@ -79,21 +85,29 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter { private async handleRequest(req: Request, res: Response) { if (!this.isOpen) { - res.status(503).send('Server is closed'); + this.coreContext.context.logger.log(`[OneBot] [HTTP Server Adapter] Server is closed`); + res.json(OB11Response.error('Server is closed', 200)); return; } + let payload = req.body; + if (req.method == 'get') { + payload = req.query; + } else if (req.query) { + payload = { ...req.query, ...req.body }; + } + const actionName = req.path.split('/')[1]; const action = this.actionMap.get(actionName); if (action) { try { - const result = await action.handle(req.body); + const result = await action.handle(payload); res.json(result); - } catch (error) { - res.status(500).send('Internal Server Error'); + } catch (error: any) { + res.json(OB11Response.error(error?.stack?.toString() || error?.message || 'Error Handle', 200)); } } else { - res.status(404).send('Action not found'); + res.json(OB11Response.error('不支持的api ' + actionName, 200)); } } }