From 4f8eaca8e1a91aaa85ed469ef329fb692cb48ff8 Mon Sep 17 00:00:00 2001 From: Jakob Gegeniger Date: Fri, 16 Oct 2026 23:26:33 +0200 Subject: [PATCH] step: context base route --- doc/docker-compose.yml | 2 +- docker-compose.yml | 6 ++++-- index.html | 1 + public/config.js | 6 ++++++ server.js | 8 ++++++++ src/services/appConfig.ts | 21 +++++++++++++++++++++ src/services/mqtt.ts | 13 ++++++++----- src/views/Home.vue | 2 +- src/views/Maintenance.vue | 4 +--- 9 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 public/config.js create mode 100644 src/services/appConfig.ts diff --git a/doc/docker-compose.yml b/doc/docker-compose.yml index 6704492..9517829 100644 --- a/doc/docker-compose.yml +++ b/doc/docker-compose.yml @@ -5,7 +5,7 @@ services: container_name: web-king-arth-maintenance restart: unless-stopped environment: - - VITE_APP_ENV=production + MQTT_TOPIC_ROOT: "outdoor/king-arthur/sword/" networks: default: diff --git a/docker-compose.yml b/docker-compose.yml index 1511094..2068f4b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,8 +6,10 @@ services: # ports: # - "80:80" # - "443:443" - # environment: - # - VITE_APP_ENV=production + # MQTT topic root: staging default lives in the app; the productive + # system strips the staging/ prefix via this environment variable. + environment: + MQTT_TOPIC_ROOT: "outdoor/king-arthur/sword/" networks: default: diff --git a/index.html b/index.html index 2145ca1..3c11a58 100644 --- a/index.html +++ b/index.html @@ -8,6 +8,7 @@
+ diff --git a/public/config.js b/public/config.js new file mode 100644 index 0000000..49448ac --- /dev/null +++ b/public/config.js @@ -0,0 +1,6 @@ +// Local development defaults, served by the Vite dev server. +// In Docker, server.js serves /config.js dynamically from the +// MQTT_TOPIC_ROOT environment variable instead (this copy is shadowed). +window.__APP_CONFIG__ = { + mqttTopicRoot: 'staging/outdoor/king-arthur/sword/' +}; diff --git a/server.js b/server.js index 5c32e09..0c915e1 100644 --- a/server.js +++ b/server.js @@ -5,6 +5,14 @@ import { fileURLToPath } from 'url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const app = express(); +// Runtime app configuration, served as window.__APP_CONFIG__. +// Must be registered before the static/catch-all routes. +const mqttTopicRoot = process.env.MQTT_TOPIC_ROOT || 'staging/outdoor/king-arthur/sword/'; +app.get('/config.js', (req, res) => { + res.type('application/javascript').send(`window.__APP_CONFIG__ = ${JSON.stringify({ mqttTopicRoot })};`); +}); +console.log(`MQTT topic root: ${mqttTopicRoot}`); + app.use(express.static(path.join(__dirname, 'dist'))); app.get('*', (req, res) => { diff --git a/src/services/appConfig.ts b/src/services/appConfig.ts new file mode 100644 index 0000000..f99d04b --- /dev/null +++ b/src/services/appConfig.ts @@ -0,0 +1,21 @@ +interface AppConfig { + mqttTopicRoot?: string +} + +declare global { + interface Window { + __APP_CONFIG__?: AppConfig + } +} + +const defaultTopicRoot = 'staging/outdoor/king-arthur/sword/' + +/** + * Runtime configuration injected by the server (/config.js -> window.__APP_CONFIG__). + * Falls back to the Vite build-time env var, then to the staging default. + */ +export function getMqttTopicRoot(): string { + const configured = window.__APP_CONFIG__?.mqttTopicRoot ?? import.meta.env.VITE_MQTT_TOPIC_ROOT + const topicRoot = configured || defaultTopicRoot + return topicRoot.endsWith('/') ? topicRoot : topicRoot + '/' +} diff --git a/src/services/mqtt.ts b/src/services/mqtt.ts index cd34dfd..cfac16d 100644 --- a/src/services/mqtt.ts +++ b/src/services/mqtt.ts @@ -1,10 +1,11 @@ import mqtt from 'mqtt' +import { getMqttTopicRoot } from './appConfig' const mqttBrokerHost = 'mqtt.excaliburs-erben.de' const mqttBrokerPort = 443 // 9001 if not secure const mqttBrokerPath = '/mqtt' const mqttUseTls = true -const mqttTopicRoot = 'staging/outdoor/king-arthur/sword/' +const mqttTopicRoot = getMqttTopicRoot() const mqttMessage = 'start-hit' const mqttUser = "exodususer"; const mqttPassword = "awesomeexodusoutdooradventures"; @@ -138,8 +139,8 @@ export function enterMaintananceMode( const verMqttTopic = mqttTopicRoot + 'ver' client.on('connect', () => { - publishCmd(client, "SetMaintanance") - client.subscribe(statMqttTopic, { qos: 1 }, (error) => { + publishCmd(client, "setMaintanance") + client.subscribe([statMqttTopic, verMqttTopic], { qos: 1 }, (error) => { if (error) { console.error('MQTT subscribe failed:', error) } @@ -166,6 +167,7 @@ export function enterMaintananceMode( } if (topic === verMqttTopic) { const messageStr = message.toString() + console.log(messageStr) verCallback(messageStr) } }) @@ -175,8 +177,9 @@ export function enterMaintananceMode( export function exitMaintananceMode(client: mqtt.MqttClient): void { publishCmd(client, "Exit") - const mqttTopic = mqttTopicRoot + 'stat' - client.unsubscribe(mqttTopic, (error) => { + const statMqttTopic = mqttTopicRoot + 'stat' + const verMqttTopic = mqttTopicRoot + 'ver' + client.unsubscribe([statMqttTopic, verMqttTopic], (error) => { if (error) { console.error('MQTT unsubscribe failed:', error) } diff --git a/src/views/Home.vue b/src/views/Home.vue index 043d7a3..2c03e56 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -7,7 +7,7 @@

Version

-

0.9.0

+

1.0.0

Service Hotline

diff --git a/src/views/Maintenance.vue b/src/views/Maintenance.vue index 99624a5..4744659 100644 --- a/src/views/Maintenance.vue +++ b/src/views/Maintenance.vue @@ -190,6 +190,4 @@ onUnmounted(() => { }) - +