1919import fs from 'fs' ;
2020import path from 'path' ;
2121import https from 'https' ;
22+ import http from 'http' ;
2223import { fileURLToPath , parse } from 'url' ;
2324import next from 'next' ;
2425
@@ -35,31 +36,50 @@ if (fs.existsSync(requiredServerFilesConfig)) {
3536 process . env . __NEXT_PRIVATE_STANDALONE_CONFIG = JSON . stringify ( nextConfig ) ;
3637}
3738
38- const PORT = 9090 ;
39- const HOST = 'localhost' ;
39+ const PORT = process . env . PORT || 9090 ;
40+ const HOST = process . env . HOST || 'localhost' ;
41+ const ENABLE_HTTPS = process . env . ENABLE_HTTPS !== 'false' ;
4042const keyPath = path . resolve ( __dirname , 'server.key' ) ;
4143const certPath = path . resolve ( __dirname , 'server.cert' ) ;
4244const dev = process . env . NODE_ENV === 'development' ;
4345const app = next ( { dev : dev , dir : __dirname } ) ;
44- const httpsOptions = {
45- key : fs . readFileSync ( keyPath ) ,
46- cert : fs . readFileSync ( certPath ) ,
47- } ;
46+
47+ // HTTPS options - only used if ENABLE_HTTPS is true
48+ let httpsOptions = null ;
49+ if ( ENABLE_HTTPS ) {
50+ try {
51+ httpsOptions = {
52+ key : fs . readFileSync ( keyPath ) ,
53+ cert : fs . readFileSync ( certPath ) ,
54+ } ;
55+ } catch ( error ) {
56+ console . error ( `Failed to load SSL certificates: ${ error . message } ` ) ;
57+ console . error ( 'Either provide valid certificates or set ENABLE_HTTPS=false' ) ;
58+ process . exit ( 1 ) ;
59+ }
60+ }
4861
4962const handle = app . getRequestHandler ( ) ;
5063
5164function getTimestampWithOffset ( ) {
5265 const now = new Date ( ) ;
5366
54- const pad = ( n ) => n . toString ( ) . padStart ( 2 , '0' ) ;
67+ const pad = n => n . toString ( ) . padStart ( 2 , '0' ) ;
5568
56- const isoDate = now . getFullYear ( ) + '-' +
57- pad ( now . getMonth ( ) + 1 ) + '-' +
58- pad ( now . getDate ( ) ) + 'T' +
59- pad ( now . getHours ( ) ) + ':' +
60- pad ( now . getMinutes ( ) ) + ':' +
61- pad ( now . getSeconds ( ) ) + '.' +
62- now . getMilliseconds ( ) . toString ( ) . padStart ( 3 , '0' ) ;
69+ const isoDate =
70+ now . getFullYear ( ) +
71+ '-' +
72+ pad ( now . getMonth ( ) + 1 ) +
73+ '-' +
74+ pad ( now . getDate ( ) ) +
75+ 'T' +
76+ pad ( now . getHours ( ) ) +
77+ ':' +
78+ pad ( now . getMinutes ( ) ) +
79+ ':' +
80+ pad ( now . getSeconds ( ) ) +
81+ '.' +
82+ now . getMilliseconds ( ) . toString ( ) . padStart ( 3 , '0' ) ;
6383
6484 const offsetMin = now . getTimezoneOffset ( ) ;
6585 const offsetSign = offsetMin <= 0 ? '+' : '-' ;
@@ -70,16 +90,28 @@ function getTimestampWithOffset() {
7090 return `${ isoDate } ${ tzOffset } ` ;
7191}
7292
73- console . log ( `Starting WSO2 Thunder gate app in ${ dev ? 'development' : 'production' } mode...` ) ;
93+ console . log (
94+ `Starting WSO2 Thunder gate app in ${ dev ? 'development' : 'production' } mode with ${ ENABLE_HTTPS ? 'HTTPS' : 'HTTP' } ...` ,
95+ ) ;
7496
7597app . prepare ( ) . then ( ( ) => {
76- https . createServer ( httpsOptions , ( req , res ) => {
98+ const requestHandler = ( req , res ) => {
7799 const parsedUrl = parse ( req . url , true ) ;
78100 handle ( req , res , parsedUrl ) ;
79- } ) . listen ( PORT , ( ) => {
101+ } ;
102+
103+ let server ;
104+ if ( ENABLE_HTTPS && httpsOptions ) {
105+ server = https . createServer ( httpsOptions , requestHandler ) ;
106+ } else {
107+ server = http . createServer ( requestHandler ) ;
108+ }
109+
110+ server . listen ( PORT , HOST , ( ) => {
80111 const isoWithOffset = getTimestampWithOffset ( ) ;
112+ const protocol = ENABLE_HTTPS ? 'https' : 'http' ;
81113 console . log (
82- `time=${ isoWithOffset } level=INFO msg="WSO2 Thunder gate app started..." address=${ HOST } :${ PORT } `
114+ `time=${ isoWithOffset } level=INFO msg="WSO2 Thunder gate app started..." address=${ protocol } :// ${ HOST } :${ PORT } ` ,
83115 ) ;
84116 } ) ;
85117} ) ;
0 commit comments