Skip to content

Commit 92a6d2e

Browse files
committed
feat: support https
1 parent 2b68ab6 commit 92a6d2e

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ router.get('/swagger.json', swaggerRoutes.swaggerJson());
126126
router.get('/swagger', swaggerRoutes.swagger());
127127
```
128128

129+
> 可参考:https://editor.swagger.io/
130+
129131
访问接口文档
130132

131133
```js

src/plugins/configSchema.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ module.exports = {
1515
description: '后端服务独立配置. ( object )',
1616
type: 'object',
1717
},
18+
https: {
19+
description: '是否支持 https 配置. ( boolean | object )',
20+
anyOf: [
21+
{
22+
type: 'boolean',
23+
},
24+
{
25+
type: 'object',
26+
},
27+
],
28+
},
1829
},
1930
required: [
2031
'entry',

src/plugins/factory/index.js

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

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

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

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+
}
62+
return Promise.all(ps).then(ress => {
63+
const res = { ...ress[0] };
64+
res.https = ress[1];
65+
return ress[0]; // 只返回 http 配置
66+
});
67+
};
68+
69+
function createServer(app, { protocol, host, port }, options) {
4670
return new Promise((resolve, reject) => {
47-
app.listen(_port, _host, err => {
71+
const errCb = err => {
4872
if (err) {
4973
return reject(err);
5074
}
51-
resolve({ host: _host, port: _port, url: `http://${_host}:${_port}` });
75+
resolve({ host, port, url: `${protocol}://${host}:${port}` });
5276

5377
if (process.env.DOCS_SWAGGER) {
54-
logger.info('[Swagger UI]', `http://${_host}:${_port}/api/docs/swagger`);
78+
logger.info('[Swagger UI]', `${protocol}://${host}:${port}/api/docs/swagger`);
79+
}
80+
};
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());
5588
}
56-
});
89+
client.listen(port, errCb);
90+
} else if (protocol === 'http') {
91+
const http = require('http');
92+
http.createServer(app.callback()).listen(port, errCb);
93+
} else {
94+
reject(new Error(`Not Support protocol: ${protocol}!`));
95+
}
5796
});
58-
};
97+
}

0 commit comments

Comments
 (0)