@@ -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