22 * Module dependencies.
33 */
44const chalk = require ( 'chalk' ) ;
5- const http = require ( 'http' ) ;
6- const https = require ( 'https' ) ;
5+ const nodeHttp = require ( 'http' ) ;
6+ const nodeHttps = require ( 'https' ) ;
77
88const config = require ( '../config' ) ;
99const mongooseService = require ( './services/mongoose' ) ;
@@ -104,6 +104,7 @@ exports.start = async () => {
104104 let db ;
105105 let orm ;
106106 let app ;
107+ let http ;
107108
108109 try {
109110 ( { db, orm, app } = await bootstrap ( ) ) ;
@@ -112,15 +113,36 @@ exports.start = async () => {
112113 }
113114
114115 try {
115- if ( config . secure && config . secure . credentials ) await https . createServer ( config . secure . credentials , app ) . listen ( config . port , config . host ) ;
116- else await http . createServer ( app ) . listen ( config . port , config . host ) ;
116+ if ( config . secure && config . secure . credentials ) http = await nodeHttps . createServer ( config . secure . credentials , app ) . listen ( config . port , config . host ) ;
117+ else http = await nodeHttp . createServer ( app ) . listen ( config . port , config . host ) ;
117118 logConfiguration ( ) ;
118119 return {
119120 db,
120121 orm,
121122 app,
123+ http,
122124 } ;
123125 } catch ( e ) {
124126 throw new Error ( e ) ;
125127 }
126128} ;
129+
130+ // Shut down the server
131+ exports . shutdown = async ( server ) => {
132+ try {
133+ server . then ( async ( value ) => {
134+ await mongooseService . disconnect ( ) ;
135+ // add sequelize
136+ value . http . close ( ( err ) => {
137+ console . info ( chalk . yellow ( 'Server closed' ) ) ;
138+ if ( err ) {
139+ console . info ( chalk . red ( 'Error on server close.' , err ) ) ;
140+ process . exitCode = 1 ;
141+ }
142+ process . exit ( ) ;
143+ } ) ;
144+ } ) ;
145+ } catch ( e ) {
146+ throw new Error ( e ) ;
147+ }
148+ } ;
0 commit comments