@@ -53,7 +53,8 @@ func main() {
5353 if err != nil {
5454 log .Printf ("Error getting absolute path for config: %v. File watching disabled." , err )
5555 } else {
56- go watchConfigFile (absConfigPath , proxyManager )
56+ var proxyManagerPtr = & proxyManager
57+ go watchConfigFile (absConfigPath , proxyManagerPtr , listenStr )
5758 }
5859 }
5960
@@ -73,8 +74,8 @@ func main() {
7374 }
7475}
7576
76- // watchConfigFile monitors the configuration file for changes and triggers a reload .
77- func watchConfigFile (configPath string , pm * proxy.ProxyManager ) {
77+ // watchConfigFile monitors the configuration file for changes and recreates the ProxyManager with the new config .
78+ func watchConfigFile (configPath string , pm * * proxy.ProxyManager , listenStr * string ) {
7879 watcher , err := fsnotify .NewWatcher ()
7980 if err != nil {
8081 log .Printf ("Error creating file watcher: %v. File watching disabled." , err )
@@ -109,11 +110,31 @@ func watchConfigFile(configPath string, pm *proxy.ProxyManager) {
109110 }
110111 debounceTimer = time .AfterFunc (debounceDuration , func () {
111112 log .Printf ("Config file modified: %s, attempting reload..." , event .Name )
112- if err := pm .ReloadConfig (configPath ); err != nil {
113- log .Printf ("Error reloading config: %v" , err )
114- } else {
115- log .Println ("Config reloaded successfully." )
113+
114+ // Load new configuration
115+ newConfig , err := proxy .LoadConfig (configPath )
116+ if err != nil {
117+ log .Printf ("Error loading new config: %v" , err )
118+ return
116119 }
120+
121+ // Stop and cleanup old ProxyManager
122+ oldPM := * pm
123+ log .Println ("Shutting down old ProxyManager instance before reload..." )
124+ oldPM .Shutdown ()
125+
126+ // Create new ProxyManager with new config
127+ newPM := proxy .New (newConfig )
128+ * pm = newPM
129+
130+ // Start serving with new ProxyManager
131+ go func () {
132+ if err := newPM .Run (* listenStr ); err != nil {
133+ log .Printf ("Server error after config reload: %v" , err )
134+ }
135+ }()
136+
137+ log .Println ("Config reloaded successfully." )
117138 })
118139 }
119140 case err , ok := <- watcher .Errors :
0 commit comments