@@ -26,10 +26,13 @@ import (
2626)
2727
2828func init () {
29+ // Register a function runner to initialize the remote configuration setup.
2930 gs .B .FuncRunner (initRemoteConfig ).OnProfiles ("online" )
3031}
3132
32- // initRemoteConfig initializes the remote configuration setup
33+ // initRemoteConfig initializes the remote configuration setup.
34+ // It first attempts to retrieve remote config, then starts a background job
35+ // to periodically refresh the configuration.
3336func initRemoteConfig () error {
3437 if err := getRemoteConfig (); err != nil {
3538 return err
@@ -38,7 +41,36 @@ func initRemoteConfig() error {
3841 return nil
3942}
4043
41- // refreshRemoteConfig periodically refreshes the remote configuration
44+ // getRemoteConfig fetches and writes the remote configuration to a local file.
45+ // It creates necessary directories and generates a properties file containing.
46+ func getRemoteConfig () error {
47+ err := os .MkdirAll ("./conf/remote" , os .ModePerm )
48+ if err != nil {
49+ return err
50+ }
51+
52+ const data = `
53+ server.addr=0.0.0.0:9090
54+
55+ log.access.name=access.log
56+ log.access.dir=./log
57+
58+ log.biz.name=biz.log
59+ log.biz.dir=./log
60+
61+ log.dao.name=dao.log
62+ log.dao.dir=./log
63+
64+ refresh_time=%v
65+ `
66+
67+ const file = "conf/remote/app-online.properties"
68+ str := fmt .Sprintf (data , time .Now ().UnixMilli ())
69+ return os .WriteFile (file , []byte (str ), os .ModePerm )
70+ }
71+
72+ // refreshRemoteConfig runs a continuous loop to periodically update configuration.
73+ // It refreshes every 500ms until context cancellation.
4274func refreshRemoteConfig (ctx context.Context ) error {
4375 for {
4476 select {
@@ -58,29 +90,3 @@ func refreshRemoteConfig(ctx context.Context) error {
5890 }
5991 }
6092}
61-
62- // getRemoteConfig fetches and writes the remote configuration to a local file
63- func getRemoteConfig () error {
64- err := os .MkdirAll ("./conf/remote" , os .ModePerm )
65- if err != nil {
66- return err
67- }
68-
69- const data = `
70- server.addr=0.0.0.0:9090
71-
72- log.access.name=access.log
73- log.access.dir=./log
74-
75- log.biz.name=biz.log
76- log.biz.dir=./log
77-
78- log.dao.name=dao.log
79- log.dao.dir=./log
80-
81- refresh_time=%v`
82-
83- const file = "conf/remote/app-online.properties"
84- str := fmt .Sprintf (data , time .Now ().UnixMilli ())
85- return os .WriteFile (file , []byte (str ), os .ModePerm )
86- }
0 commit comments