iching-broker-level1/broker.ts

83 lines
2 KiB
TypeScript
Raw Normal View History

2025-03-25 08:54:22 +00:00
import { exec } from 'child_process';
2025-03-25 11:39:58 +00:00
const nodemailer = require('nodemailer');
2025-03-25 08:54:22 +00:00
exec('iching divine', (error, stdout, stderr) => {
2025-03-25 14:04:48 +00:00
console.log(`Begin`);
2025-03-25 08:54:22 +00:00
if (error) {
2025-03-25 14:04:48 +00:00
console.error(`Error: ${error.message}`);
2025-03-25 08:54:22 +00:00
return;
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
return;
}
2025-03-25 11:39:58 +00:00
//console.log(`Ergebnis:\n${stdout}`);
2025-03-25 08:54:22 +00:00
2025-03-25 14:04:48 +00:00
console.log(`Send E-Mail`);
sendEmail(stdout);
sleep();
2025-03-25 08:54:22 +00:00
});
2025-03-25 11:39:58 +00:00
// Create Transporter
2025-03-25 11:39:58 +00:00
const transporter = nodemailer.createTransport({
host: "mailhog.mailhog.svc.cluster.local", // MailHog ClusterIP
port: 1025, // Standard-MailHog SMTP-Port
secure: false // MailHog needs no encryption
});
2025-03-25 11:39:58 +00:00
// Send E-Mail
async function sendEmail(content: string) {
try {
const info = await transporter.sendMail({
from: '"Test Sender" <test@example.com>',
to: "Christopher.Hase@telekom.com;test@mailhog.local",
subject: "Horoscope from MailHog",
text: content,
2025-03-26 14:21:12 +00:00
//html: "<p> ${content} </p>"
html: html(content)
});
console.log("E-Mail sent: ", info.messageId);
} catch (error) {
console.error("Error Sending E-Mail:", error);
2025-03-25 11:39:58 +00:00
}
}
2025-03-26 14:21:12 +00:00
//Sleep. To avoid issue in pod: Completed -> Terminated -> CrashLoopBackOff
async function sleep() {
console.log("Go to sleep: ", new Date().toISOString());
// sleep 1h (3600000 ms)
setTimeout(() => {
sleep(); // recursion after 1h
}, 3600000);
}
2025-03-26 14:21:12 +00:00
function html(input: string): string {
// Zerlege den String in einzelne Zeilen
const lines = input.split("\n");
// Bearbeite jede Zeile (z.B. trimme Leerzeichen und füge einen Prefix hinzu)
2025-03-26 14:34:52 +00:00
//const processedLines = lines.map(line => `> ${line.trim()}`);
const processedLines = lines.map(line => processLine(line));
2025-03-26 14:21:12 +00:00
// Füge die Zeilen wieder zu einem String zusammen
return processedLines.join("\n");
}
//TODO: process every line...
function processLine(input: string): string {
2025-03-26 14:34:52 +00:00
return input;
2025-03-26 14:21:12 +00:00
}
2025-03-25 11:39:58 +00:00