Changed config file location for builds.
Some checks reported errors
farmcontrol/farmcontrol-server/pipeline/head Something is wrong with the build of this commit

This commit is contained in:
Tom Butcher 2025-12-28 22:09:29 +00:00
parent 7dbe7da4ee
commit 34ee72b19e

View File

@ -4,22 +4,76 @@ import path from "path";
import { fileURLToPath } from "url";
import log4js from "log4js";
// Configure paths relative to this file
// Determine environment
const NODE_ENV = process.env.NODE_ENV || "development";
// Configure paths
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const CONFIG_PATH = path.resolve(__dirname, "../config.json");
function getProductionConfigPath() {
switch (process.platform) {
case "win32":
return path.join(
process.env.PROGRAMDATA || "C:\\ProgramData",
"farmcontrol-server",
"config.json"
);
case "darwin":
// macOS standard for system-wide config is often /etc or /Library/Application Support
// We'll stick to /etc as requested for parity with Linux
return "/etc/farmcontrol-server/config.json";
case "linux":
default:
return "/etc/farmcontrol-server/config.json";
}
}
const CONFIG_PATH =
NODE_ENV === "production"
? getProductionConfigPath()
: path.resolve(__dirname, "../config.json");
const logger = log4js.getLogger("Config");
logger.level = "info";
// Determine environment
const NODE_ENV = process.env.NODE_ENV || "development";
const DEFAULT_CONFIG = {
development: {
logLevel: "debug",
url: "https://dev-wss.tombutcher.work",
apiUrl: "https://dev.tombutcher.work/api",
host: {
id: "",
authCode: "",
},
},
production: {
logLevel: "info",
url: "https://ws.farmcontrol.app",
apiUrl: "https://api.farmcontrol.app",
host: {
id: "",
authCode: "",
},
},
};
// Load config file
export function loadConfig() {
try {
if (!fs.existsSync(CONFIG_PATH)) {
throw new Error(`Configuration file not found at ${CONFIG_PATH}`);
logger.info(
`Configuration file not found at ${CONFIG_PATH}. Creating a new one.`
);
const configDir = path.dirname(CONFIG_PATH);
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
fs.writeFileSync(
CONFIG_PATH,
JSON.stringify(DEFAULT_CONFIG, null, 2),
"utf8"
);
}
const configData = fs.readFileSync(CONFIG_PATH, "utf8");
@ -33,7 +87,7 @@ export function loadConfig() {
return config[NODE_ENV];
} catch (err) {
console.error("Error loading config:", err);
logger.error("Error loading config:", err);
throw err;
}
}
@ -46,6 +100,11 @@ export function saveConfig(newConfig) {
if (fs.existsSync(CONFIG_PATH)) {
const configData = fs.readFileSync(CONFIG_PATH, "utf8");
config = JSON.parse(configData);
} else {
const configDir = path.dirname(CONFIG_PATH);
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
}
// Update current environment