iching-broker-level3/broker.ts

153 lines
4.1 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 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({
2025-03-26 14:55:41 +00:00
from: '"The Oracle" <the.oracle@holy.mountain>',
to: "Christopher.Hase@telekom.com;test@mailhog.local",
2025-03-28 08:32:23 +00:00
subject: "Your Horoscope Is Ready",
text: content,
2025-03-27 08:52:03 +00:00
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
2025-03-27 14:29:19 +00:00
//node structure for the Parse Tree (it's a degenerated tree with one or zero children per node)
2025-03-27 12:27:14 +00:00
interface Node {
type: "Root" | "Hexagram" | "Judgement" | "Images" | "ChangingLines" ;
child?: Node;
value?: string;
}
2025-03-27 14:29:19 +00:00
//generate 1) Parse Tree and 2) HTML
2025-03-27 12:27:14 +00:00
function html(inputText: string): string {
const parseTree = parse(inputText);
const htmlOutput = render(parseTree);
return htmlOutput;
}
2025-03-27 14:29:19 +00:00
//generate the Parse Tree
2025-03-27 12:27:14 +00:00
function parse(input: string): Node {
console.log("Parse input text");
2025-03-26 14:21:12 +00:00
2025-03-27 13:59:49 +00:00
const root: Node = { type: "Root"};
2025-03-27 12:27:14 +00:00
var currentNode: Node = root;
2025-03-26 14:21:12 +00:00
2025-03-27 12:27:14 +00:00
const lines = input.split("\n");
2025-03-26 14:21:12 +00:00
2025-03-27 12:27:14 +00:00
for (const line of lines) {
if (line.startsWith("Hexagram")) {
const hexagram: Node = { type: "Hexagram"};
currentNode.child = hexagram;
currentNode = hexagram;
2025-03-27 14:29:19 +00:00
currentNode.value = "<h1>" + line + "</h1>";
2025-03-27 13:59:49 +00:00
} else if (line.startsWith("Judgement")) {
2025-03-27 12:27:14 +00:00
const judgement: Node = { type: "Judgement"};
currentNode.child = judgement;
currentNode = judgement;
2025-03-27 13:59:49 +00:00
currentNode.value = "<h2>" + line + "</h2>";
} else if (line.startsWith("Images")) {
2025-03-27 12:27:14 +00:00
const images: Node = { type: "Images"};
currentNode.child = images;
currentNode = images;
2025-03-27 13:59:49 +00:00
currentNode.value = "<h2>" + line + "</h2>";
} else if (line.startsWith("~") && currentNode.type != "ChangingLines") {
2025-03-27 12:27:14 +00:00
const changingLines: Node = { type: "ChangingLines"};
currentNode.child = changingLines;
currentNode = changingLines;
2025-03-27 14:29:19 +00:00
currentNode.value = line + "<br>";
2025-03-27 13:59:49 +00:00
} else {
2025-03-27 13:42:28 +00:00
currentNode.value = currentNode.value + line + "<br>";
2025-03-27 12:27:14 +00:00
}
}
2025-03-26 14:21:12 +00:00
2025-03-27 12:27:14 +00:00
return root;
2025-03-26 14:21:12 +00:00
}
2025-03-27 14:29:19 +00:00
//generate HTML from Parse Tree
2025-03-27 12:27:14 +00:00
function render(node: Node): string {
2025-03-27 13:01:33 +00:00
if (node == undefined) {
2025-03-27 12:36:53 +00:00
console.log("...finished...")
return "";
}
2025-03-27 13:59:49 +00:00
console.log("Render node" + node.type);
2025-03-27 12:27:14 +00:00
var outputHTML: string = "";
switch (node.type) {
case "Root":
return render(node.child!);
2025-03-27 14:29:19 +00:00
case "Hexagram":
2025-03-28 08:56:05 +00:00
//node.value = node.value?.replace("</h1>", "</h1><h3>");
2025-03-28 09:13:47 +00:00
node.value = node.value?.replace("</h1>", "</h1><div style=\"border: 1px solid gray; padding: 15px; width: 300px; text-align: center; box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.2);\"><h3>");
2025-03-28 08:48:54 +00:00
node.value = node.value?.replace("<br>", " - ");
2025-03-28 09:10:15 +00:00
//outputHTML = "<p>" + node.value + "</h3></p>";
outputHTML = "<p>" + node.value + "</h3></div></p>";
2025-03-27 14:29:19 +00:00
outputHTML = outputHTML + render(node.child!);
return outputHTML;
2025-03-27 13:59:49 +00:00
case "ChangingLines" :
outputHTML = "<p style=\"color: gray;\">" + node.value + "</p>";
outputHTML = outputHTML + render(node.child!);
return outputHTML;
2025-03-27 12:27:14 +00:00
default:
outputHTML = "<p>" + node.value + "</p>";
2025-03-27 13:23:36 +00:00
outputHTML = outputHTML + render(node.child!);
2025-03-27 12:27:14 +00:00
2025-03-27 13:23:36 +00:00
return outputHTML;
2025-03-27 12:27:14 +00:00
}
2025-03-26 14:21:12 +00:00
}
2025-03-25 11:39:58 +00:00