chore: add ecosystem.config

This commit is contained in:
scarqin 2023-03-15 11:45:56 +08:00
parent 361bc9af8a
commit d67d7395a3
8 changed files with 124 additions and 100 deletions

View File

@ -0,0 +1,13 @@
module.exports = {
apps: [
{
name: 'extension-runtime',
script: 'main.js',
watch: '.'
}
],
deploy: {
production: {}
}
};

View File

@ -6,8 +6,8 @@
"scripts": {
"prepare": "node tools/install.cjs",
"dev": "nodemon main",
"start": "pm2 start main.js",
"stop": "pm2 stop main.js",
"start": "pm2 start ecosystem.config.cjs",
"stop": "pm2 stop ecosystem.config.cjs",
"log": "pm2 logs"
},
"type": "module",

View File

@ -0,0 +1,17 @@
module.exports = {
apps: [
{
name: 'http-server',
script: 'server/main.js',
watch: '.'
},
{
name: 'websocket-server',
script: 'server/socketio.js',
watch: '.'
}
],
deploy: {
production: {}
}
};

View File

@ -5,12 +5,9 @@
"main": "./server/main.js",
"scripts": {
"dev": "node ./server/main.js",
"start": "pm2-runtime start ./server/main.js",
"start:io": "pm2-runtime start ./server/socketio.js",
"start:all": "yarn start && yarn start:io",
"start:all:pm2": "pm2 start ./server/main.js",
"start": "pm2 start ecosystem.config.js",
"watch": "pm2 list",
"stop": "pm2 stop all",
"stop": "pm2 stop ecosystem.config.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Postcat",

View File

@ -8,7 +8,6 @@ const _LibsCommon = require('../request/libs/common.js');
const koaBody = require('koa-body');
const Koa = require('koa');
const cors = require('@koa/cors');
const socketio = require('./socketio.js');
const app = new Koa();
const port = process.env.TEST_SERVER_PORT || 4201;
@ -46,6 +45,5 @@ app.use(async (ctx, next) => {
next();
});
socketio();
app.listen(port);
console.log(`Server is running at port ${port} ...`);

View File

@ -8,100 +8,98 @@ process.on('uncaughtException', err => {
const _post = process.env.EOAPI_WEBSOCKET_PORT || 13928;
const socket = (port = _post) => {
const io = new IO.Server(port, {
transports: ['websocket']
});
io.on('connection', socket => {
// * send a message to the client
socket.emit('ws-client', 'link success');
let ws = null;
const io = new IO.Server(port, {
transports: ['websocket']
});
io.on('connection', socket => {
// * send a message to the client
socket.emit('ws-client', 'link success');
let ws = null;
const unlisten = () => {
if (!ws) return null;
ws.on('close', () => null);
ws.on('upgrade', () => null);
ws.on('message', () => null);
const unlisten = () => {
if (!ws) return null;
ws.on('close', () => null);
ws.on('upgrade', () => null);
ws.on('message', () => null);
ws = null;
};
socket.on('grpc-server', async data => {
// * 创建 grpc 客户端发起请求
// 端口管理、编译文件、运行插件代码、销毁服务
const [res, err] = await grpcClient(data);
socket.emit('grpc-client', [res, err]);
});
// receive a message from the client
socket.on('ws-server', ({ type, content }) => {
if (type === 'connect') {
return;
}
if (type === 'ws-disconnect') {
ws.close();
ws = null;
};
socket.on('grpc-server', async data => {
// * 创建 grpc 客户端发起请求
// 端口管理、编译文件、运行插件代码、销毁服务
const [res, err] = await grpcClient(data);
socket.emit('grpc-client', [res, err]);
});
// receive a message from the client
socket.on('ws-server', ({ type, content }) => {
if (type === 'connect') {
return;
}
if (type === 'ws-disconnect') {
ws.close();
ws = null;
return;
}
if (type === 'ws-connect') {
const { request } = content;
const link = /^(wss:\/{2})|(ws:\/{2})\S+$/m.test(request.uri.trim())
? request.uri.trim()
: request.protocol + '://' + request.uri.trim().replace('//', '');
try {
ws = new WebSocket(link, {
headers: request?.requestParams.headerParams
?.filter(it => it.name && it.paramAttr?.example)
.reduce(
(total, { name, paramAttr }) => ({
...total,
[name]: paramAttr.example
}),
{}
)
});
} catch (error) {
socket.emit('ws-client', { type: 'ws-connect-back', status: -1, content: error });
}
ws.on('error', err => {
socket.emit('ws-client', { type: 'ws-connect-back', status: -1, content: err });
unlisten();
return;
}
if (type === 'ws-connect') {
const { request } = content;
const link = /^(wss:\/{2})|(ws:\/{2})\S+$/m.test(request.uri.trim())
? request.uri.trim()
: request.protocol + '://' + request.uri.trim().replace('//', '');
try {
ws = new WebSocket(link, {
headers: request?.requestParams.headerParams
?.filter(it => it.name && it.paramAttr?.example)
.reduce(
(total, { name, paramAttr }) => ({
...total,
[name]: paramAttr.example
}),
{}
)
});
const reqHeader = ws._req.getHeaders();
// 打开WebSocket连接后立刻发送一条消息:
ws.on('open', () => {
// console.log(`[CLIENT] open()`);
});
ws.on('upgrade', ({ headers: resHeader }) => {
socket.emit('ws-client', { type: 'ws-connect-back', status: 0, content: { reqHeader, resHeader } });
});
ws.on('message', message => {
socket.emit('ws-client', {
type: 'ws-message-back',
status: 0,
content: message?.toString() || message
});
});
// ws.on('close', () => {
// socket.emit('ws-client', {
// type: 'ws-connect-back',
// status: -1,
// content: 'Server disconnected'
// });
// unlisten();
// });
} catch (error) {
socket.emit('ws-client', { type: 'ws-connect-back', status: -1, content: error });
}
if (type === 'ws-message') {
const { message } = content;
if (!message) {
console.log('发送内容为空');
}
ws.send(message);
ws.on('error', err => {
socket.emit('ws-client', { type: 'ws-connect-back', status: -1, content: err });
unlisten();
});
const reqHeader = ws._req.getHeaders();
// 打开WebSocket连接后立刻发送一条消息:
ws.on('open', () => {
// console.log(`[CLIENT] open()`);
});
ws.on('upgrade', ({ headers: resHeader }) => {
socket.emit('ws-client', { type: 'ws-connect-back', status: 0, content: { reqHeader, resHeader } });
});
ws.on('message', message => {
socket.emit('ws-client', {
type: 'ws-message-back',
status: 0,
content: message?.toString() || message
});
});
// ws.on('close', () => {
// socket.emit('ws-client', {
// type: 'ws-connect-back',
// status: -1,
// content: 'Server disconnected'
// });
// unlisten();
// });
}
if (type === 'ws-message') {
const { message } = content;
if (!message) {
console.log('发送内容为空');
}
});
ws.send(message);
}
});
};
});
module.exports = socket;

View File

@ -17,7 +17,7 @@ import path from 'node:path';
const extServerMap = new Map<string, SidebarView>();
// * npm pkg name
const defaultExtension = [{ name: 'postcat-export-openapi' }, { name: 'postcat-import-openapi' }];
const defaultExtension = [{ name: 'postcat-export-openapi' }, { name: 'postcat-import-openapi' }, { name: 'postcat-basic-auth' }];
const isExists = async filePath =>
await promises
.access(filePath)

View File

@ -32,7 +32,8 @@
"scripts/build.js",
"test/e2e/test.ts",
"test/e2e/test.ts",
"src/node/extensions-manage/extension-manage.js"
"src/node/extensions-manage/extension-manage.js",
"src/node/test-server/ecosystem.config.js"
],
"exclude": ["node_modules", "**/*.spec.ts", "**/browser/**/*.js", "**/browser/**/*.ts", "out"],
"angularCompilerOptions": {