forked from yydfys/CatPawOpen
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
79 lines (76 loc) · 2.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import fastify from 'fastify';
import router from './router.js';
import {JsonDB, Config} from 'node-json-db';
import axios from 'axios';
import {findAvailablePort} from './util/tool.js';
let server = null;
/**
* Start the server with the given configuration.
*
* Be careful that start will be called multiple times when
* work with catvodapp. If the server is already running,
* the stop will be called by engine before start, make sure
* to return new server every time.
*
* @param {Map} config - the config of the server
* @return {void}
*/
export async function start(config) {
/**
* @type {import('fastify').FastifyInstance}
*/
server = fastify({
serverFactory: catServerFactory,
forceCloseConnections: true,
logger: !!(process.env.NODE_ENV !== 'development'),
maxParamLength: 10240,
});
server.messageToDart = async (data, inReq) => {
try {
if (!data.prefix) {
data.prefix = inReq ? inReq.server.prefix : '';
}
console.log(data);
const port = catDartServerPort();
if (port == 0) {
return null;
}
const resp = await axios.post(`http://127.0.0.1:${port}/msg`, data);
return resp.data;
} catch (error) {
return null;
}
};
server.address = function () {
const result = this.server.address();
result.url = `http://${result.address}:${result.port}`;
result.dynamic = 'js2p://_WEB_';
return result;
};
server.addHook('onError', async (_request, _reply, error) => {
console.error(error);
if (!error.statusCode) error.statusCode = 500;
return error;
});
server.stop = false;
server.config = config;
// 推荐使用NODE_PATH做db存储的更目录,这个目录在应用中清除缓存时会被清空
server.db = new JsonDB(new Config((process.env['NODE_PATH'] || '.') + '/db.json', true, true, '/', true));
server.register(router);
const startPort = process.env['DEV_HTTP_PORT'] || 5758;
const availablePort = await findAvailablePort(startPort);
// 注意 一定要监听ipv4地址 build后 app中使用时 端口使用0让系统自动分配可用端口
// server.listen({port: process.env['DEV_HTTP_PORT'] || 0, host: '::'});
server.listen({port: availablePort, host: '::'});
}
/**
* Stop the server if it exists.
*
*/
export async function stop() {
if (server) {
server.close();
server.stop = true;
}
server = null;
}