From 03e81435e232f135ed448c4837f6366d6cf62ad4 Mon Sep 17 00:00:00 2001 From: FunctionsAPI Date: Tue, 28 Jan 2025 11:58:02 +0000 Subject: [PATCH] Automatic push from FunctionsAPI --- README.md | 3 +-- index.js | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 index.js diff --git a/README.md b/README.md index e9eaeb3..ac78096 100644 --- a/README.md +++ b/README.md @@ -1,2 +1 @@ -# f9e3a96e7f774500bedaec5cf761dd60 - +# commonjs hello-world diff --git a/index.js b/index.js new file mode 100644 index 0000000..2ec82e3 --- /dev/null +++ b/index.js @@ -0,0 +1,78 @@ +const amqp = require('amqplib'); // For RabbitMQ +const { v4: uuidv4 } = require('uuid'); // Generate unique IDs for messages +const winston = require('winston'); // For logging + +// Configure logging using winston +const logger = winston.createLogger({ + level: 'info', + format: winston.format.combine( + winston.format.timestamp(), + winston.format.printf(({ timestamp, level, message }) => `${timestamp} - ${level.toUpperCase()}: ${message}`) + ), + transports: [ + new winston.transports.Console(), + new winston.transports.File({ filename: 'app.log' }) + ] +}); + +// RabbitMQ Configuration +const RABBITMQ_URL = 'amqp://localhost'; +const EXCHANGE_NAME = 'df.metadata'; +const ROUTING_KEY = 'metadata-sink'; + +// Mock Resource Object +const resourceData = { + columns: [], + dataSetId: "01234567890123456789abcd", + description: "description", + isEnriched: false, + name: "name", + organizationId: "2dceb541-a1c1-4f38-96a4-9052dd69133e", + projectId: "01234567890123456789abcd", + resourceId: "a9aecd83-058e-4d6f-9fb9-f62a0e86246c", + resourceType: "asset", + tags: [] +}; + +async function sendMessageToRabbitMQ() { + let connection; + try { + // Step 1: Connect to RabbitMQ + connection = await amqp.connect(RABBITMQ_URL); + const channel = await connection.createChannel(); + + // Step 2: Assert the exchange + await channel.assertExchange(EXCHANGE_NAME, 'topic', { durable: true }); + + // Step 3: Publish the message + const message = { + id: uuidv4(), + timestamp: new Date().toISOString(), + resource: resourceData + }; + const messageBuffer = Buffer.from(JSON.stringify(message)); + + const result = channel.publish(EXCHANGE_NAME, ROUTING_KEY, messageBuffer); + + // Logging results + if (result) { + logger.info('Message successfully published to RabbitMQ'); + } else { + logger.warn('Message was not confirmed by RabbitMQ'); + } + + // Close the channel + await channel.close(); + } catch (error) { + logger.error(`Error sending message to RabbitMQ: ${error.message}`); + } finally { + if (connection) { + await connection.close(); + } + } +} + +// Main Execution +sendMessageToRabbitMQ().catch((error) => { + logger.error(`Unexpected error occurred: ${error.message}`); +}); \ No newline at end of file