mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
chore: 鉴权和回复
This commit is contained in:
parent
e9e517533a
commit
9afc0f6667
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user