Skip to content

Commit de62bb1

Browse files
committed
feat: 调整创建顺序逻辑,并将 server 挂载在 app 上
1 parent 794a50e commit de62bb1

File tree

3 files changed

+56
-36
lines changed

3 files changed

+56
-36
lines changed

demo/microapp/config/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = {
1919
abc: 123,
2020
},
2121
cc: 1,
22+
// http2: true,
2223
},
2324
devServer: {
2425
port: 6666,

src/plugins/factory/_customResult.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ module.exports = function(app) {
5050
json.path = this.path;
5151
}
5252

53-
if (this.path && this.path.startsWith('/api')) {
54-
const version = this.path.replace(/^\/?api\/?/, '').split('/')[0];
53+
// api 前缀匹配后,会提取 version 字段
54+
if (this.path && this.path.startsWith('/api') && this.params) {
55+
const version = this.params.version;
5556
if (version) {
5657
json.version = version;
5758
}

src/plugins/factory/index.js

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,17 @@ module.exports = async function(api, info = {}) {
2020
// 上下文参数
2121
const apiContext = api.context || {};
2222

23-
const { index, port, host, entries = [], https = false } = info;
23+
const { index, port, host, entries = [], https = false, http2 = false } = info;
24+
25+
const protocol = https ? 'https' : 'http';
26+
27+
let server;
28+
if (https) {
29+
server = createServer(app, { protocol, isOpenHttp2: http2 }, typeof https === 'object' && https);
30+
} else {
31+
server = createServer(app, { protocol, isOpenHttp2: http2 });
32+
}
33+
2434
if (entries.length > 0) {
2535
await entries.reduce((chain, entry) => {
2636
const runApp = require(entry); // app.js
@@ -34,39 +44,56 @@ module.exports = async function(api, info = {}) {
3444
}
3545

3646
const portfinder = require('portfinder');
37-
3847
const startPort = parseInt(process.env.PORT || apiContext.port || port || 3000);
3948
const _port = await portfinder.getPortPromise({
4049
port: startPort, // minimum port
4150
stopPort: startPort + 300, // maximum port
4251
});
43-
4452
const _host = process.env.HOST || apiContext.host || host || '0.0.0.0';
4553

46-
// const supportProtocols = ['http', 'https'];
47-
// app.listen(_port, _host, err => {
48-
// if (err) {
49-
// return reject(err);
50-
// }
51-
// resolve({ host: _host, port: _port, url: `http://${_host}:${_port}` });
52-
53-
// if (process.env.DOCS_SWAGGER) {
54-
// logger.info('[Swagger UI]', `http://${_host}:${_port}/api/docs/swagger`);
55-
// }
56-
// });
57-
58-
const ps = [ createServer(app, { protocol: 'http', host: _host, port: _port }) ];
59-
if (https) {
60-
ps.push(createServer(app, { protocol: 'https', host: _host, port: +_port + 1 }, typeof https === 'object' && https));
61-
}
54+
const ps = [ listen(server, { protocol, host: _host, port: _port }) ];
6255
return Promise.all(ps).then(ress => {
6356
const res = { ...ress[0] };
64-
res.https = ress[1];
65-
return ress[0]; // 只返回 http 配置
57+
return res; // 只返回第一个配置
6658
});
6759
};
6860

69-
function createServer(app, { protocol, host, port }, options) {
61+
function createServer(app, { protocol, isOpenHttp2 }, options) {
62+
let server = null;
63+
if (protocol === 'https') {
64+
const https = require('https');
65+
if (options) {
66+
if (isOpenHttp2) {
67+
const http2 = require('http2');
68+
server = http2.createSecureServer(options, app.callback());
69+
} else {
70+
server = https.createServer(options, app.callback());
71+
}
72+
} else {
73+
if (isOpenHttp2) {
74+
throw new Error('https "options" argument must be of type object.');
75+
}
76+
server = https.createServer(app.callback());
77+
}
78+
} else if (protocol === 'http') {
79+
if (isOpenHttp2) {
80+
const http2 = require('http2');
81+
server = http2.createServer(app.callback());
82+
} else {
83+
const http = require('http');
84+
server = http.createServer(app.callback());
85+
}
86+
}
87+
88+
if (!server) {
89+
throw new Error(`Not Support protocol: ${protocol}!`);
90+
}
91+
92+
app.server = server;
93+
return server;
94+
}
95+
96+
function listen(server, { protocol, host, port }) {
7097
return new Promise((resolve, reject) => {
7198
const errCb = err => {
7299
if (err) {
@@ -78,18 +105,9 @@ function createServer(app, { protocol, host, port }, options) {
78105
logger.info('[Swagger UI]', `${protocol}://${host}:${port}/api/docs/swagger`);
79106
}
80107
};
81-
if (protocol === 'https') {
82-
const https = require('https');
83-
let client = null;
84-
if (options) {
85-
client = https.createServer(options, app.callback());
86-
} else {
87-
client = https.createServer(app.callback());
88-
}
89-
client.listen(port, errCb);
90-
} else if (protocol === 'http') {
91-
const http = require('http');
92-
http.createServer(app.callback()).listen(port, errCb);
108+
109+
if (server) {
110+
server.listen(port, errCb);
93111
} else {
94112
reject(new Error(`Not Support protocol: ${protocol}!`));
95113
}

0 commit comments

Comments
 (0)