@@ -12,16 +12,44 @@ let config;
1212let useSSL = false ;
1313let 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 } ;
1545try {
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