Skip to content

Commit 9b565f6

Browse files
committed
rework config server load to use user generated files
1 parent 7f10ff2 commit 9b565f6

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

tests/perceptor-host-test/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,6 @@ keys/
151151
*.cert
152152
*.p12
153153
*.pfx
154+
155+
# User configuration files
156+
config.json
File renamed without changes.

tests/perceptor-host-test/server.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,44 @@ let config;
1212
let useSSL = false;
1313
let protocols = ["ws"];
1414

15+
// Load defaults first
16+
let defaultConfig = {
17+
hostname: "localhost",
18+
port: 8080
19+
};
20+
21+
try {
22+
const defaultConfigFile = path.join(__dirname, "config_defaults.json");
23+
const defaultConfigData = fs.readFileSync(defaultConfigFile, "utf8");
24+
defaultConfig = JSON.parse(defaultConfigData);
25+
} catch (err) {
26+
console.warn("Failed to load config_defaults.json, using built-in defaults:", err.message);
27+
}
28+
29+
// Handle user config
30+
const configFile = path.join(__dirname, "config.json");
31+
const configExists = fs.existsSync(configFile);
32+
33+
if (!configExists) {
34+
// Create config.json from defaults if it doesn't exist
35+
try {
36+
fs.writeFileSync(configFile, JSON.stringify(defaultConfig, null, 2));
37+
console.log("Created config.json from config_defaults.json");
38+
} catch (err) {
39+
console.warn("Failed to create config.json:", err.message);
40+
}
41+
}
42+
43+
// Load user config and merge with defaults
44+
config = { ...defaultConfig };
1545
try {
16-
const configFile = path.join(__dirname, "config.json");
1746
const configData = fs.readFileSync(configFile, "utf8");
18-
config = JSON.parse(configData);
47+
const userConfig = JSON.parse(configData);
48+
config = { ...defaultConfig, ...userConfig };
49+
console.log("Loaded user config from config.json");
1950
} catch (err) {
20-
console.error("Failed to load config.json, using defaults:", err);
21-
config = {
22-
hostname: "localhost",
23-
port: 8080
24-
};
51+
console.error("Failed to load config.json:", err.message);
52+
config = defaultConfig;
2553
}
2654

2755
// Try to load SSL certificates if specified in config

0 commit comments

Comments
 (0)