Skip to content

Commit

Permalink
feat: support https
Browse files Browse the repository at this point in the history
  • Loading branch information
zyao89 committed Aug 25, 2021
1 parent 2b68ab6 commit 92a6d2e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ router.get('/swagger.json', swaggerRoutes.swaggerJson());
router.get('/swagger', swaggerRoutes.swagger());
```

> 可参考:https://editor.swagger.io/
访问接口文档

```js
Expand Down
11 changes: 11 additions & 0 deletions src/plugins/configSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ module.exports = {
description: '后端服务独立配置. ( object )',
type: 'object',
},
https: {
description: '是否支持 https 配置. ( boolean | object )',
anyOf: [
{
type: 'boolean',
},
{
type: 'object',
},
],
},
},
required: [
'entry',
Expand Down
51 changes: 45 additions & 6 deletions src/plugins/factory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = async function(api, info = {}) {
// 上下文参数
const apiContext = api.context || {};

const { index, port, host, entries = [] } = info;
const { index, port, host, entries = [], https = false } = info;
if (entries.length > 0) {
await entries.reduce((chain, entry) => {
const runApp = require(entry); // app.js
Expand All @@ -43,16 +43,55 @@ module.exports = async function(api, info = {}) {

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));
}
return Promise.all(ps).then(ress => {
const res = { ...ress[0] };
res.https = ress[1];
return ress[0]; // 只返回 http 配置
});
};

function createServer(app, { protocol, host, port }, options) {
return new Promise((resolve, reject) => {
app.listen(_port, _host, err => {
const errCb = err => {
if (err) {
return reject(err);
}
resolve({ host: _host, port: _port, url: `http://${_host}:${_port}` });
resolve({ host, port, url: `${protocol}://${host}:${port}` });

if (process.env.DOCS_SWAGGER) {
logger.info('[Swagger UI]', `http://${_host}:${_port}/api/docs/swagger`);
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);
} else {
reject(new Error(`Not Support protocol: ${protocol}!`));
}
});
};
}

0 comments on commit 92a6d2e

Please sign in to comment.