- Introduced a new NatsServer class for managing NATS connections and messaging. - Implemented connection handling, publishing, requesting, and subscription management. - Updated index.js to connect to NATS on application startup and handle connection errors. - Enhanced logging for NATS operations to improve traceability and error management.
61 lines
1.5 KiB
JavaScript
61 lines
1.5 KiB
JavaScript
import { loadConfig } from './config.js';
|
|
import { SocketManager } from './socket/socketmanager.js';
|
|
import { etcdServer } from './database/etcd.js';
|
|
import { natsServer } from './database/nats.js';
|
|
import express from 'express';
|
|
import log4js from 'log4js';
|
|
import http from 'http';
|
|
import { mongoServer } from './database/mongo.js';
|
|
|
|
(async () => {
|
|
// Load configuration
|
|
const config = loadConfig();
|
|
|
|
// Setup logger
|
|
const logger = log4js.getLogger('FarmControl API WS');
|
|
logger.level = config.server.logLevel;
|
|
|
|
// Create Express app and HTTP server
|
|
const app = express();
|
|
const server = http.createServer(app);
|
|
|
|
new SocketManager(server);
|
|
|
|
// Connect to Etcd (await)
|
|
try {
|
|
await etcdServer.connect();
|
|
logger.info('Connected to Etcd');
|
|
} catch (err) {
|
|
logger.error('Failed to connect to Etcd:', err);
|
|
throw err;
|
|
}
|
|
|
|
// Connect to NATS (await)
|
|
try {
|
|
await natsServer.connect();
|
|
logger.info('Connected to NATS');
|
|
} catch (err) {
|
|
logger.error('Failed to connect to NATS:', err);
|
|
throw err;
|
|
}
|
|
|
|
// Connect to Mongo DB (await)
|
|
try {
|
|
await mongoServer.connect();
|
|
logger.info('Connected to Mongo DB');
|
|
} catch (err) {
|
|
logger.error('Failed to connect to Mongo DB:', err);
|
|
throw err;
|
|
}
|
|
|
|
// Start HTTP server
|
|
server.listen(config.server.port, () => {
|
|
logger.info(`Server listening on port ${config.server.port}`);
|
|
});
|
|
|
|
process.on('SIGINT', async () => {
|
|
logger.info('Shutting down...');
|
|
await etcdServer.disconnect();
|
|
});
|
|
})();
|