This repository has been archived on 2026-10-08. You can view files and clone it, but cannot push or open issues or pull requests.
allotment/packages/server/mqtt/loadConfig.js

30 lines
825 B
JavaScript

import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import config from "../config.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const defaultMqttPath = path.join(__dirname, "..", "config", "mqtt.json");
export function loadMqttConfig() {
const configured = config.mqtt?.configPath?.trim();
const configPath = configured || defaultMqttPath;
if (!fs.existsSync(configPath)) {
return null;
}
const raw = fs.readFileSync(configPath, "utf-8");
return JSON.parse(raw);
}
export function resolveBrokerPassword(config) {
const envKey = config?.broker?.passwordEnv;
if (envKey && process.env[envKey]) {
return process.env[envKey];
}
if (config?.broker?.password) {
return config.broker.password;
}
return process.env.MQTT_PASSWORD || "";
}