diff --git a/README.md b/README.md index 9de1b6b..6db6456 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,16 @@ This app will send an I-Ching horoscope to the pre-configured mailhog instance i This app uses the I-Ching library app https://github.com/Velfi/i-ching.git. -## Configuration + + +## First Start +The app can be deployed by running: + +```bash +$ kubectl apply -f deployment.yaml +``` This will start the deployment of the app in the pod, the service and an ingress for the HTTP frontend server and an ingress for the backend server. When a pod with the app is initally started, one email will be sent to the configured receiver. @@ -31,35 +38,10 @@ The backend server has a separate ingress, so API requests can get forwarded to The frontend server is running inside the pod on the address http://localhost:8080/iching. It provides an HTML homepage with a button. Upon pressing the button, the backend server will be called (to send an E-Mail). The frontend server has a separate ingress from the backend server, since the frontend server needs the 'rewrite target' annotation: So the Frontend is not running in the root folder but using the sub-path 'iching'. -## Testing + \ No newline at end of file diff --git a/backend/broker.test.ts b/backend/broker.test.ts index 555a47a..c4232d1 100644 --- a/backend/broker.test.ts +++ b/backend/broker.test.ts @@ -1,5 +1,5 @@ -import { html } from "./broker"; -//import { test } from "@jest/globals"; +/*import { html } from "./broker"; +import { test } from "@jest/globals"; jest.useFakeTimers(); @@ -17,3 +17,4 @@ test("Generate HTML for images", () => { expect(html("Images:\nOver the earth, the lake:\nThe image of Gathering Together.\nThus the superior man renews his weapons\nIn order to meet the unforseen.")) .toBe("

Images:

Over the earth, the lake:
The image of Gathering Together.
Thus the superior man renews his weapons
In order to meet the unforseen.

"); }); +*/ \ No newline at end of file diff --git a/backend/broker.ts b/backend/broker.ts index e672cb6..391235b 100644 --- a/backend/broker.ts +++ b/backend/broker.ts @@ -1,15 +1,13 @@ import { exec } from 'child_process'; import * as fs from 'fs'; import * as nodemailer from 'nodemailer'; -import { fileURLToPath } from 'url'; -import * as path from 'path'; // Config for scheduling the next time the main process (sending of horoscope) is run -interface Config { +/*interface Config { emailReceiver: string; mailHost: string; mailPort: number; -} +}*/ // Node structure for the Parse Tree (it's a degenerated tree with one or zero children per node) interface Node { @@ -18,13 +16,13 @@ interface Node { value?: string; } -var config : Config; +//var config : Config; export function executeCommand(): void { exec('iching divine', (error, stdout, stderr) => { - console.log(`I-Ching-Broker: \'iching divine\' called`); + console.log(`Begin`); if (error) { console.error(`Error: ${error.message}`); @@ -37,11 +35,11 @@ export function executeCommand(): void { } //Load config once - if (config == undefined) { + /*if (config == undefined) { config = loadConfig(); - } + } */ - console.log(`Horoscope received; sending E-Mail next`); + console.log(`Send E-Mail`); sendEmail(stdout); }); } @@ -50,43 +48,36 @@ export function executeCommand(): void { executeCommand(); // Load the Configuration -function loadConfig(): Config { +/*function loadConfig(): Config { console.log(`Load Config`); - const __filename = fileURLToPath(import.meta.url); - const __dirname = path.dirname(__filename); - const configPath = path.join(__dirname, 'config.json'); - - const data = fs.readFileSync(configPath, 'utf-8'); - + const data = fs.readFileSync('config.json', 'utf-8'); return JSON.parse(data); -} +}*/ // Send E-Mail async function sendEmail(content: string) { // Create Transporter const transporter = nodemailer.createTransport({ - host: config.mailHost, //"mailhog.mailhog.svc.cluster.local", //config.mailHost, - port: config.mailPort, //1025, //config.mailPort, + host: "mailhog.mailhog.svc.cluster.local", //config.mailHost, + port: 1025, //config.mailPort, secure: false }); try { const info = await transporter.sendMail({ from: '"The Oracle" ', - to: config.emailReceiver, //"test@mailhog.local", //config.emailReceiver, + to: "test@mailhog.local", //config.emailReceiver, subject: "Your Horoscope Is Ready", text: content, html: html(content) }); - console.log("E-Mail sent: ", info.messageId + "\n\n"); + console.log("E-Mail sent: ", info.messageId + "\n"); } catch (error) { - console.error("Error Sending E-Mail:", error + "\n\n"); - - console.log("Failed to send this horoscope: \n", content + "\n"); + console.error("Error Sending E-Mail:", error); } } @@ -128,7 +119,7 @@ function parse(input: string): Node { const changingLines: Node = { type: "ChangingLines"}; currentNode.child = changingLines; currentNode = changingLines; - currentNode.value = line; + currentNode.value = line; // + "
"; TODO: try without this
} else { currentNode.value = currentNode.value + line + "
"; } @@ -141,11 +132,13 @@ function parse(input: string): Node { function render(node: Node): string { if (node == undefined) { - console.log("I-Ching-Broker: Rendering of nodes finished!") + console.log("Rendering of nodes finished!") return ""; } + console.log("Render node" + node.type); + var outputHTML: string = ""; switch (node.type) { @@ -168,6 +161,7 @@ function render(node: Node): string { case "ChangingLines" : const regex = new RegExp("~", "g"); node.value = node.value?.replace(regex, ""); + //outputHTML = "

" + node.value + "


"; outputHTML = "

" + node.value + "


"; outputHTML = outputHTML + render(node.child!); return outputHTML; diff --git a/backend/server.ts b/backend/server.ts index 4a76fc7..c045e0e 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -8,13 +8,10 @@ const port = 8090; app.use(cors()); app.post('/iching/api/command', (req, res) => { - //TODO no logging from inside this method??? - console.log(`Backend-Server: receiving POST to /iching/api/command`); - executeCommand(); - res.status(200).send('Backend-Server: Broker was called\n'); + res.status(200).send('Command executed\n'); }); app.listen(port, '0.0.0.0', () => { - console.log(`Backend-Server running on http://0.0.0.0:${port}`); + console.log(`Server läuft auf http://0.0.0.0:${port}`); }); diff --git a/deployment.yaml b/deployment.yaml index 6e77607..e75a70c 100644 --- a/deployment.yaml +++ b/deployment.yaml @@ -1,11 +1,3 @@ -#env: -# - name: DEPLOY_MODE -# value: "kubernetes" -# - name: BACKEND_PORT -# value: "8090" - -#--- - # --- DEPLOYMENT -------------------------------------------- apiVersion: apps/v1 diff --git a/frontend/deploy.js b/frontend/deploy.js deleted file mode 100644 index 12e8f02..0000000 --- a/frontend/deploy.js +++ /dev/null @@ -1 +0,0 @@ -window.DEPLOY_MODE = "k8s" \ No newline at end of file diff --git a/frontend/event.ts b/frontend/event.ts index 9b464a3..4a7a778 100644 --- a/frontend/event.ts +++ b/frontend/event.ts @@ -1,20 +1,10 @@ function handleClick(): void { console.log("Der Button wurde geklickt!"); - //const deployMode = (window as { DEPLOY_MODE?: 'docker' | 'k8s' }).DEPLOY_MODE ?? 'k8s'; - const deployMode = (window as { DEPLOY_MODE?: 'docker' | 'k8s' | 'local' }).DEPLOY_MODE ?? 'k8s'; - - const endpoint = - deployMode === 'docker' || deployMode === 'local' - ? 'http://localhost:8090/iching/api/command' - : '/iching/api/command'; - - console.log(`DEPLOY_MODE=${deployMode}, sende POST an: ${endpoint}`); - - fetch(endpoint, { method: 'POST' }) + fetch("/iching/api/command", { method: "POST" }) .then(res => res.text()) - .then(text => console.log("HTTP-Server says:", text)) - .catch(error => console.error("HTTP-Server - Error:", error)); + .then(text => console.log("Server sagt:", text)) + .catch(error => console.error("Fehler:", error)); } document.addEventListener("DOMContentLoaded", () => { diff --git a/frontend/index.html b/frontend/index.html index 9c6984a..e3a618b 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -53,15 +53,12 @@
-

Do you want to know the future?

-

☝️ Look into the future with your personal I-Ging Horoscope! ☝️

+

🧘 My I-Ging Horoscope

Click on the Button to receive your personal Horoscope. 100% accuracy guaranteed!

- +
- - \ No newline at end of file diff --git a/old.jest.config.ts b/jest.config.ts similarity index 100% rename from old.jest.config.ts rename to jest.config.ts diff --git a/package.json b/package.json index ce50417..d28af51 100644 --- a/package.json +++ b/package.json @@ -24,9 +24,5 @@ "express": "^5.1.0", "http-server": "^14.1.1", "nodemailer": "^6.10.0" - }, - "jest": { - "preset": "ts-jest", - "testEnvironment": "node" - } + } } diff --git a/start-local.sh b/start-local.sh deleted file mode 100755 index 93cdbc5..0000000 --- a/start-local.sh +++ /dev/null @@ -1,38 +0,0 @@ - -# build -#npm install -#npx tsc -p tsconfig.backend.json -#npx tsc -p tsconfig.frontend.json - -#npm run start:local - -#!/bin/bash - -# Exit on error -set -e - -# 1. Install dependencies -npm install - -# 2. Compile backend and frontend -npx tsc -p tsconfig.backend.json -npx tsc -p tsconfig.frontend.json - -# 3. Ensure dist/frontend exists and copy index.html -mkdir -p dist/frontend -cp frontend/index.html dist/frontend/ - -mkdir -p dist/backend -cp backend/config.json dist/backend/ - -# 4. Rename compiled frontend event.js → event.mjs -find dist/frontend -name "event.js" -exec bash -c 'mv "$0" "${0%.js}.mjs"' {} \; - -# 5. Inject deploy mode -echo "window.DEPLOY_MODE = 'local';" > dist/frontend/deploy.js - -# 6. Start backend -node dist/backend/server.js & - -# 7. Start frontend -npx http-server dist/frontend -p 8080 --cors --mime application/javascript=js diff --git a/start.sh b/start.sh old mode 100755 new mode 100644 index 1efc7c5..a48ebb0 --- a/start.sh +++ b/start.sh @@ -1,13 +1,3 @@ -#!/bin/bash - -# Setze Standard-Wert auf "k8s" wenn DEPLOY_MODE nicht gesetzt ist -DEPLOY_MODE=${DEPLOY_MODE:-k8s} - -echo "DEPLOY_MODE ist: $DEPLOY_MODE" - -# Injektiere ins Frontend -echo "window.DEPLOY_MODE = \"$DEPLOY_MODE\";" > dist/frontend/deploy.js - # start backend in the background node dist/backend/server.js & diff --git a/tsconfig.backend.json b/tsconfig.backend.json index c5bc144..db21beb 100644 --- a/tsconfig.backend.json +++ b/tsconfig.backend.json @@ -8,6 +8,7 @@ "esModuleInterop": true, "skipLibCheck": true }, - "include": ["backend/**/*.ts"], - "exclude": ["backend/**/*.test.ts"] + "include": [ + "backend/**/*.ts" + ] } \ No newline at end of file