wwebjs-api/server.js
M Firdaus Riawan 364c8fe647
fix: ensure environment variables are loaded correctly in Docker (#49)
* feat: add reverse proxy support with configurable base path and trust proxy settings

* fix: ensure environment variables are loaded correctly in Docker

- Move dotenv config to the top of server.js
- Update docker-compose.yml to properly handle .env file
- Remove duplicate environment variables section

This ensures BASE_PATH and TRUST_PROXY work as expected in both Docker and local environments.
2025-09-06 14:30:01 +03:00

36 lines
1.3 KiB
JavaScript

// Load environment variables first
require('dotenv').config({ path: process.env.ENV_PATH || '.env' })
const app = require('./src/app')
const { baseWebhookURL, enableWebHook, enableWebSocket, autoStartSessions } = require('./src/config')
const { logger } = require('./src/logger')
const { handleUpgrade } = require('./src/websocket')
const { restoreSessions } = require('./src/sessions')
// Start the server
const port = process.env.PORT || 3000
// Check if BASE_WEBHOOK_URL environment variable is available when WebHook is enabled
if (!baseWebhookURL && enableWebHook) {
logger.error('BASE_WEBHOOK_URL environment variable is not set. Exiting...')
process.exit(1) // Terminate the application with an error code
}
const server = app.listen(port, () => {
logger.info(`Server running on port ${port}`)
logger.debug({ configuration: require('./src/config') }, 'Service configuration')
if (autoStartSessions) {
logger.info('Starting all sessions')
restoreSessions()
}
})
if (enableWebSocket) {
server.on('upgrade', (request, socket, head) => {
handleUpgrade(request, socket, head)
})
}
// puppeteer uses subscriptions to SIGINT, SIGTERM, and SIGHUP to know when to close browser instances
// this disables the warnings when you starts more than 10 browser instances
process.setMaxListeners(0)