11const fs = require ( 'fs' ) ;
22const ConfigLoader = require ( './ConfigLoader' ) ;
3- const proxy = require ( '../proxy' ) ;
43const { validate } = require ( './file' ) ; // Import the validate function
54
65const defaultSettings = require ( '../../proxy.config.json' ) ;
@@ -17,43 +16,6 @@ let _config = { ...defaultSettings, ...(_userSettings || {}) };
1716// Create config loader instance
1817const configLoader = new ConfigLoader ( _config ) ;
1918
20- // Handle configuration updates
21- configLoader . on ( 'configurationChanged' , async ( newConfig ) => {
22- console . log ( 'Configuration updated from external source' ) ;
23- try {
24- // 1. Stop existing services
25- await proxy . stop ( ) ;
26-
27- // 2. Update config
28- _config = newConfig ;
29-
30- // 3. Validate new configuration
31- validate ( ) ; // Use the imported validate function
32-
33- // 4. Restart services with new config
34- await proxy . start ( ) ;
35-
36- console . log ( 'Services restarted with new configuration' ) ;
37- } catch ( error ) {
38- console . error ( 'Failed to apply new configuration:' , error ) ;
39- // Attempt to restart with previous config
40- try {
41- await proxy . start ( ) ;
42- } catch ( startError ) {
43- console . error ( 'Failed to restart services:' , startError ) ;
44- }
45- }
46- } ) ;
47-
48- configLoader . on ( 'configurationError' , ( error ) => {
49- console . error ( 'Error loading external configuration:' , error ) ;
50- } ) ;
51-
52- // Start the config loader if external sources are enabled
53- configLoader . start ( ) . catch ( ( error ) => {
54- console . error ( 'Failed to start configuration loader:' , error ) ;
55- } ) ;
56-
5719// Helper function to get current config value
5820const getConfig = ( key ) => {
5921 return _config [ key ] ;
@@ -109,6 +71,50 @@ const logConfiguration = () => {
10971 console . log ( `authentication = ${ JSON . stringify ( getAuthentication ( ) ) } ` ) ;
11072} ;
11173
74+ // Function to handle configuration updates
75+ const handleConfigUpdate = async ( newConfig ) => {
76+ console . log ( 'Configuration updated from external source' ) ;
77+ try {
78+ // 1. Get proxy module dynamically to avoid circular dependency
79+ const proxy = require ( '../proxy' ) ;
80+
81+ // 2. Stop existing services
82+ await proxy . stop ( ) ;
83+
84+ // 3. Update config
85+ _config = newConfig ;
86+
87+ // 4. Validate new configuration
88+ validate ( ) ;
89+
90+ // 5. Restart services with new config
91+ await proxy . start ( ) ;
92+
93+ console . log ( 'Services restarted with new configuration' ) ;
94+ } catch ( error ) {
95+ console . error ( 'Failed to apply new configuration:' , error ) ;
96+ // Attempt to restart with previous config
97+ try {
98+ const proxy = require ( '../proxy' ) ;
99+ await proxy . start ( ) ;
100+ } catch ( startError ) {
101+ console . error ( 'Failed to restart services:' , startError ) ;
102+ }
103+ }
104+ } ;
105+
106+ // Handle configuration updates
107+ configLoader . on ( 'configurationChanged' , handleConfigUpdate ) ;
108+
109+ configLoader . on ( 'configurationError' , ( error ) => {
110+ console . error ( 'Error loading external configuration:' , error ) ;
111+ } ) ;
112+
113+ // Start the config loader if external sources are enabled
114+ configLoader . start ( ) . catch ( ( error ) => {
115+ console . error ( 'Failed to start configuration loader:' , error ) ;
116+ } ) ;
117+
112118// Force reload of configuration
113119const reloadConfiguration = async ( ) => {
114120 await configLoader . reloadConfiguration ( ) ;
0 commit comments