From 92a6d2e914f2af37b865c02052401f0184985b57 Mon Sep 17 00:00:00 2001 From: Zyao89 Date: Wed, 25 Aug 2021 15:36:22 +0800 Subject: [PATCH] feat: support https --- README.md | 2 ++ src/plugins/configSchema.js | 11 ++++++++ src/plugins/factory/index.js | 51 +++++++++++++++++++++++++++++++----- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 49205e8..a6cbf0e 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,8 @@ router.get('/swagger.json', swaggerRoutes.swaggerJson()); router.get('/swagger', swaggerRoutes.swagger()); ``` +> 可参考:https://editor.swagger.io/ + 访问接口文档 ```js diff --git a/src/plugins/configSchema.js b/src/plugins/configSchema.js index 4d4cf20..26e0e38 100644 --- a/src/plugins/configSchema.js +++ b/src/plugins/configSchema.js @@ -15,6 +15,17 @@ module.exports = { description: '后端服务独立配置. ( object )', type: 'object', }, + https: { + description: '是否支持 https 配置. ( boolean | object )', + anyOf: [ + { + type: 'boolean', + }, + { + type: 'object', + }, + ], + }, }, required: [ 'entry', diff --git a/src/plugins/factory/index.js b/src/plugins/factory/index.js index 9199427..ad18ad5 100644 --- a/src/plugins/factory/index.js +++ b/src/plugins/factory/index.js @@ -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 @@ -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}!`)); + } }); -}; +}