@@ -55,23 +55,39 @@ app.use('/materials', express.static(path.join(__dirname, '../uploads/materials'
5555// Database initialization
5656const initializeDatabase = async ( ) => {
5757 try {
58+ // Log database connection parameters (redacting password)
59+ console . log ( `[LOG database] ========= Attempting to connect to database:` ) ;
60+ console . log ( `[LOG database] ========= Host: ${ process . env . DB_HOST } ` ) ;
61+ console . log ( `[LOG database] ========= Database: ${ process . env . DB_NAME } ` ) ;
62+ console . log ( `[LOG database] ========= User: ${ process . env . DB_USER } ` ) ;
63+ console . log ( `[LOG database] ========= Port: ${ process . env . DB_PORT || '3306' } ` ) ;
64+
5865 await sequelize . authenticate ( ) ;
59- console . log ( 'Database connection has been established successfully.' ) ;
66+ console . log ( '[LOG database] ========= Database connection has been established successfully.' ) ;
6067
6168 // In development, sync tables without dropping them
6269 if ( process . env . NODE_ENV === 'development' ) {
63- console . log ( 'Running in development mode - syncing database tables' ) ;
70+ console . log ( '[LOG database] ========= Running in development mode - syncing database tables' ) ;
6471 await sequelize . sync ( ) ;
65- console . log ( 'Database tables synced successfully.' ) ;
72+ console . log ( '[LOG database] ========= Database tables synced successfully.' ) ;
6673 }
6774
68-
75+ return true ;
6976 } catch ( error ) {
70- console . error ( 'Unable to connect to the database:' , error ) ;
77+ console . error ( '[LOG database] ========= Unable to connect to the database:' , error ) ;
78+
7179 // Don't exit process in production as it will crash the serverless function
7280 if ( process . env . NODE_ENV !== 'production' ) {
81+ // Just log in cPanel environment without exiting
82+ if ( process . env . CPANEL === 'true' ) {
83+ console . error ( '[LOG database] ========= Running in cPanel environment - continuing despite database error' ) ;
84+ return false ;
85+ }
86+ console . error ( '[LOG database] ========= Exiting application due to database connection failure' ) ;
7387 process . exit ( 1 ) ;
7488 }
89+
90+ return false ;
7591 }
7692} ;
7793
@@ -196,15 +212,21 @@ const PORT = process.env.PORT || 3000;
196212
197213// Initialize database in production without server.listen
198214if ( process . env . NODE_ENV === 'production' ) {
199- initializeDatabase ( ) . catch ( err => console . error ( 'Database init error:' , err ) ) ;
215+ initializeDatabase ( )
216+ . then ( success => {
217+ console . log ( `[LOG server] ========= Production server started. Database connection: ${ success ? 'SUCCESS' : 'FAILED' } ` ) ;
218+ } )
219+ . catch ( err => console . error ( '[LOG database] ========= Database init error:' , err ) ) ;
200220}
201221// Only start the server if we're not in a test or production environment
202222else if ( process . env . NODE_ENV !== 'test' ) {
203- initializeDatabase ( ) . then ( ( ) => {
204- app . listen ( PORT , ( ) => {
205- console . log ( `Server is running on port ${ PORT } ` ) ;
206- console . log ( `Swagger documentation available at http://localhost:${ PORT } /api-docs` ) ;
207- } ) ;
223+ initializeDatabase ( ) . then ( success => {
224+ if ( success || process . env . CPANEL === 'true' ) {
225+ app . listen ( PORT , ( ) => {
226+ console . log ( `[LOG server] ========= Server is running on port ${ PORT } ` ) ;
227+ console . log ( `[LOG server] ========= Swagger documentation available at http://localhost:${ PORT } /api-docs` ) ;
228+ } ) ;
229+ }
208230 } ) ;
209231}
210232
0 commit comments