Skip to content

Commit

Permalink
feat: 调整创建顺序逻辑,并将 server 挂载在 app 上
Browse files Browse the repository at this point in the history
  • Loading branch information
zyao89 committed Feb 16, 2022
1 parent 794a50e commit de62bb1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
1 change: 1 addition & 0 deletions demo/microapp/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
abc: 123,
},
cc: 1,
// http2: true,
},
devServer: {
port: 6666,
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/factory/_customResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ module.exports = function(app) {
json.path = this.path;
}

if (this.path && this.path.startsWith('/api')) {
const version = this.path.replace(/^\/?api\/?/, '').split('/')[0];
// api 前缀匹配后,会提取 version 字段
if (this.path && this.path.startsWith('/api') && this.params) {
const version = this.params.version;
if (version) {
json.version = version;
}
Expand Down
86 changes: 52 additions & 34 deletions src/plugins/factory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ module.exports = async function(api, info = {}) {
// 上下文参数
const apiContext = api.context || {};

const { index, port, host, entries = [], https = false } = info;
const { index, port, host, entries = [], https = false, http2 = false } = info;

const protocol = https ? 'https' : 'http';

let server;
if (https) {
server = createServer(app, { protocol, isOpenHttp2: http2 }, typeof https === 'object' && https);
} else {
server = createServer(app, { protocol, isOpenHttp2: http2 });
}

if (entries.length > 0) {
await entries.reduce((chain, entry) => {
const runApp = require(entry); // app.js
Expand All @@ -34,39 +44,56 @@ module.exports = async function(api, info = {}) {
}

const portfinder = require('portfinder');

const startPort = parseInt(process.env.PORT || apiContext.port || port || 3000);
const _port = await portfinder.getPortPromise({
port: startPort, // minimum port
stopPort: startPort + 300, // maximum port
});

const _host = process.env.HOST || apiContext.host || host || '0.0.0.0';

// const supportProtocols = ['http', 'https'];
// app.listen(_port, _host, err => {
// if (err) {
// return reject(err);
// }
// resolve({ host: _host, port: _port, url: `http://${_host}:${_port}` });

// if (process.env.DOCS_SWAGGER) {
// logger.info('[Swagger UI]', `http://${_host}:${_port}/api/docs/swagger`);
// }
// });

const ps = [ createServer(app, { protocol: 'http', host: _host, port: _port }) ];
if (https) {
ps.push(createServer(app, { protocol: 'https', host: _host, port: +_port + 1 }, typeof https === 'object' && https));
}
const ps = [ listen(server, { protocol, host: _host, port: _port }) ];
return Promise.all(ps).then(ress => {
const res = { ...ress[0] };
res.https = ress[1];
return ress[0]; // 只返回 http 配置
return res; // 只返回第一个配置
});
};

function createServer(app, { protocol, host, port }, options) {
function createServer(app, { protocol, isOpenHttp2 }, options) {
let server = null;
if (protocol === 'https') {
const https = require('https');
if (options) {
if (isOpenHttp2) {
const http2 = require('http2');
server = http2.createSecureServer(options, app.callback());
} else {
server = https.createServer(options, app.callback());
}
} else {
if (isOpenHttp2) {
throw new Error('https "options" argument must be of type object.');
}
server = https.createServer(app.callback());
}
} else if (protocol === 'http') {
if (isOpenHttp2) {
const http2 = require('http2');
server = http2.createServer(app.callback());
} else {
const http = require('http');
server = http.createServer(app.callback());
}
}

if (!server) {
throw new Error(`Not Support protocol: ${protocol}!`);
}

app.server = server;
return server;
}

function listen(server, { protocol, host, port }) {
return new Promise((resolve, reject) => {
const errCb = err => {
if (err) {
Expand All @@ -78,18 +105,9 @@ function createServer(app, { protocol, host, port }, options) {
logger.info('[Swagger UI]', `${protocol}://${host}:${port}/api/docs/swagger`);
}
};
if (protocol === 'https') {
const https = require('https');
let client = null;
if (options) {
client = https.createServer(options, app.callback());
} else {
client = https.createServer(app.callback());
}
client.listen(port, errCb);
} else if (protocol === 'http') {
const http = require('http');
http.createServer(app.callback()).listen(port, errCb);

if (server) {
server.listen(port, errCb);
} else {
reject(new Error(`Not Support protocol: ${protocol}!`));
}
Expand Down

0 comments on commit de62bb1

Please sign in to comment.