@@ -2,6 +2,8 @@ const os = require('os');
22const jwt = require ( 'jsonwebtoken' ) ;
33const http = require ( 'http' )
44const https = require ( 'https' )
5+ const http2express = require ( 'http2-express' ) ;
6+ const httpolyglot = require ( '@httptoolkit/httpolyglot' ) ;
57const morgan = require ( 'morgan' ) ;
68const express = require ( 'express' )
79const cookieParser = require ( 'cookie-parser' ) ;
@@ -39,7 +41,7 @@ const metricsMiddleware = promBundle({
3941 metricType : PROMETHEUS_METRIC_TYPE ,
4042} ) ;
4143
42- const app = express ( )
44+ const app = http2express ( express ) ;
4345app . set ( 'json spaces' , 2 ) ;
4446app . set ( 'trust proxy' , trustProxy ) ;
4547
@@ -204,36 +206,65 @@ app.all('/{*splat}', (req, res) => {
204206
205207} ) ;
206208
207- let httpOpts = {
208- maxHeaderSize : maxHeaderSize
209- }
210-
211- let httpsOpts = {
209+ // let httpOpts = {
210+ // maxHeaderSize: maxHeaderSize
211+ // }
212+
213+ // let httpsOpts = {
214+ // key: require('fs').readFileSync(process.env.HTTPS_KEY_FILE || 'testpk.pem'),
215+ // cert: require('fs').readFileSync(process.env.HTTPS_CERT_FILE || 'fullchain.pem'),
216+ // maxHeaderSize: maxHeaderSize
217+ // };
218+
219+ // //Whether to enable the client certificate feature
220+ // if(process.env.MTLS_ENABLE){
221+ // httpsOpts = {
222+ // requestCert: true,
223+ // rejectUnauthorized: false,
224+ // ...httpsOpts
225+ // }
226+ // }
227+
228+ // var httpServer = http.createServer(httpOpts, app).listen(process.env.HTTP_PORT || 8080);
229+ // var httpsServer = https.createServer(httpsOpts,app).listen(process.env.HTTPS_PORT || 8443);
230+
231+ // plain text http server, http2 server (aka "h2c")
232+ var httpServer = httpolyglot . createServer ( {
233+ http : { maxHeaderSize : maxHeaderSize }
234+ } , app ) . listen ( process . env . HTTP_PORT || 8080 ) ;
235+
236+ let tlsOpts = {
212237 key : require ( 'fs' ) . readFileSync ( process . env . HTTPS_KEY_FILE || 'testpk.pem' ) ,
213238 cert : require ( 'fs' ) . readFileSync ( process . env . HTTPS_CERT_FILE || 'fullchain.pem' ) ,
214- maxHeaderSize : maxHeaderSize
239+ ALPNProtocols : [ 'http/1.1' , 'h2' ] ,
215240} ;
216241
217242//Whether to enable the client certificate feature
218243if ( process . env . MTLS_ENABLE ) {
219- httpsOpts = {
244+ tlsOpts = {
220245 requestCert : true ,
221246 rejectUnauthorized : false ,
222- ...httpsOpts
247+ ...tlsOpts
223248 }
224249}
225250
226- var httpServer = http . createServer ( httpOpts , app ) . listen ( process . env . HTTP_PORT || 8080 ) ;
227- var httpsServer = https . createServer ( httpsOpts , app ) . listen ( process . env . HTTPS_PORT || 8443 ) ;
251+ var httpsServer = httpolyglot . createServer ( {
252+ tls : tlsOpts ,
253+ http : { maxHeaderSize : maxHeaderSize }
254+ } , app ) . listen ( process . env . HTTPS_PORT || 8443 ) ;
255+
228256console . log ( `Listening on ports ${ process . env . HTTP_PORT || 8080 } for http, and ${ process . env . HTTPS_PORT || 8443 } for https.` ) ;
229257
230258let calledClose = false ;
231259
232260process . on ( 'exit' , function ( ) {
233261 if ( calledClose ) return ;
234262 console . log ( 'Got exit event. Trying to stop Express server.' ) ;
235- server . close ( function ( ) {
236- console . log ( "Express server closed" ) ;
263+ httpServer . close ( function ( ) {
264+ console . log ( "HTTP server closed" ) ;
265+ } ) ;
266+ httpsServer . close ( function ( ) {
267+ console . log ( "HTTPS server closed" ) ;
237268 } ) ;
238269} ) ;
239270
0 commit comments