From 852c5e4a94391d2d8009352bf3017ea7e7c926aa Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Wed, 26 Jul 2023 00:19:21 +0800 Subject: [PATCH] finish auto_config --- server/assistant/etc/assistant.yaml | 2 +- server/auth/etc/auth.yaml | 2 +- server/backend/etc/backend.yaml | 2 +- server/canteen/etc/canteen.yaml | 2 +- server/data-transfer/etc/data-transfer.yaml | 2 +- server/home-user-auth/etc/home-user-auth.yaml | 2 +- server/inventory/etc/inventory.yaml | 2 +- server/map-library/etc/map-library.yaml | 2 +- server/orders/etc/orders.yaml | 2 +- server/product-model/etc/product-model.yaml | 2 +- .../etc/product-template.yaml | 2 +- server/product/etc/product.yaml | 2 +- server/render/etc/render.yaml | 2 +- .../etc/shopping-cart-confirmation.yaml | 2 +- server/upload/etc/upload.yaml | 2 +- server/webset/etc/webset.yaml | 2 +- utils/auto_config/auto_config.go | 91 +++++++++++++++++++ utils/auto_config/auto_config_test.go | 7 ++ 18 files changed, 114 insertions(+), 16 deletions(-) create mode 100644 utils/auto_config/auto_config.go create mode 100644 utils/auto_config/auto_config_test.go diff --git a/server/assistant/etc/assistant.yaml b/server/assistant/etc/assistant.yaml index 3804fea1..cda59ed5 100644 --- a/server/assistant/etc/assistant.yaml +++ b/server/assistant/etc/assistant.yaml @@ -1,5 +1,5 @@ Name: assistant -Host: 0.0.0.0 +Host: localhost Port: 9950 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: diff --git a/server/auth/etc/auth.yaml b/server/auth/etc/auth.yaml index abc3b013..52364fc8 100644 --- a/server/auth/etc/auth.yaml +++ b/server/auth/etc/auth.yaml @@ -1,5 +1,5 @@ Name: auth -Host: 0.0.0.0 +Host: localhost Port: 9980 MainAddress: "http://localhost:9900" SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest diff --git a/server/backend/etc/backend.yaml b/server/backend/etc/backend.yaml index 18f08394..115183dd 100644 --- a/server/backend/etc/backend.yaml +++ b/server/backend/etc/backend.yaml @@ -1,5 +1,5 @@ Name: backend -Host: 0.0.0.0 +Host: localhost Port: 9901 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: diff --git a/server/canteen/etc/canteen.yaml b/server/canteen/etc/canteen.yaml index 4b363db8..2745f38f 100644 --- a/server/canteen/etc/canteen.yaml +++ b/server/canteen/etc/canteen.yaml @@ -1,5 +1,5 @@ Name: canteen -Host: 0.0.0.0 +Host: localhost Port: 9902 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: diff --git a/server/data-transfer/etc/data-transfer.yaml b/server/data-transfer/etc/data-transfer.yaml index 4a3e767d..fc2792fe 100644 --- a/server/data-transfer/etc/data-transfer.yaml +++ b/server/data-transfer/etc/data-transfer.yaml @@ -1,5 +1,5 @@ Name: data-transfer -Host: 0.0.0.0 +Host: localhost Port: 9903 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: diff --git a/server/home-user-auth/etc/home-user-auth.yaml b/server/home-user-auth/etc/home-user-auth.yaml index cea74d9f..876d7c70 100644 --- a/server/home-user-auth/etc/home-user-auth.yaml +++ b/server/home-user-auth/etc/home-user-auth.yaml @@ -1,5 +1,5 @@ Name: home-user-auth -Host: 0.0.0.0 +Host: localhost Port: 9904 MainAddress: "http://localhost:9900" SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest diff --git a/server/inventory/etc/inventory.yaml b/server/inventory/etc/inventory.yaml index a8195506..decc2507 100644 --- a/server/inventory/etc/inventory.yaml +++ b/server/inventory/etc/inventory.yaml @@ -1,5 +1,5 @@ Name: inventory -Host: 0.0.0.0 +Host: localhost Port: 9905 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: diff --git a/server/map-library/etc/map-library.yaml b/server/map-library/etc/map-library.yaml index 0dc22627..c86dc1fe 100644 --- a/server/map-library/etc/map-library.yaml +++ b/server/map-library/etc/map-library.yaml @@ -1,5 +1,5 @@ Name: map-library -Host: 0.0.0.0 +Host: localhost Port: 9906 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: diff --git a/server/orders/etc/orders.yaml b/server/orders/etc/orders.yaml index 77573dcf..d2d92f86 100644 --- a/server/orders/etc/orders.yaml +++ b/server/orders/etc/orders.yaml @@ -1,5 +1,5 @@ Name: orders -Host: 0.0.0.0 +Host: localhost Port: 9907 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: diff --git a/server/product-model/etc/product-model.yaml b/server/product-model/etc/product-model.yaml index 867437e2..3fcb21b7 100644 --- a/server/product-model/etc/product-model.yaml +++ b/server/product-model/etc/product-model.yaml @@ -1,5 +1,5 @@ Name: product-model -Host: 0.0.0.0 +Host: localhost Port: 9909 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: diff --git a/server/product-template/etc/product-template.yaml b/server/product-template/etc/product-template.yaml index 287b4a33..c3c2a5fe 100644 --- a/server/product-template/etc/product-template.yaml +++ b/server/product-template/etc/product-template.yaml @@ -1,5 +1,5 @@ Name: product-template -Host: 0.0.0.0 +Host: localhost Port: 9910 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: diff --git a/server/product/etc/product.yaml b/server/product/etc/product.yaml index 658e5411..12cb36a3 100644 --- a/server/product/etc/product.yaml +++ b/server/product/etc/product.yaml @@ -1,5 +1,5 @@ Name: product -Host: 0.0.0.0 +Host: localhost Port: 9908 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: diff --git a/server/render/etc/render.yaml b/server/render/etc/render.yaml index 2dba7dc7..c4866d02 100644 --- a/server/render/etc/render.yaml +++ b/server/render/etc/render.yaml @@ -1,5 +1,5 @@ Name: render -Host: 0.0.0.0 +Host: localhost Port: 8888 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: diff --git a/server/shopping-cart-confirmation/etc/shopping-cart-confirmation.yaml b/server/shopping-cart-confirmation/etc/shopping-cart-confirmation.yaml index fa40bb45..32f32ad7 100644 --- a/server/shopping-cart-confirmation/etc/shopping-cart-confirmation.yaml +++ b/server/shopping-cart-confirmation/etc/shopping-cart-confirmation.yaml @@ -1,5 +1,5 @@ Name: shopping-cart-confirmation -Host: 0.0.0.0 +Host: localhost Port: 9911 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: diff --git a/server/upload/etc/upload.yaml b/server/upload/etc/upload.yaml index 8481514f..1a07e281 100644 --- a/server/upload/etc/upload.yaml +++ b/server/upload/etc/upload.yaml @@ -1,5 +1,5 @@ Name: upload -Host: 0.0.0.0 +Host: localhost Port: 9912 SourceMysql: "fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest" Env: "test" diff --git a/server/webset/etc/webset.yaml b/server/webset/etc/webset.yaml index 4d7bb858..08d1d41c 100644 --- a/server/webset/etc/webset.yaml +++ b/server/webset/etc/webset.yaml @@ -1,5 +1,5 @@ Name: webset -Host: 0.0.0.0 +Host: localhost Port: 9913 SourceMysql: "fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest" Auth: diff --git a/utils/auto_config/auto_config.go b/utils/auto_config/auto_config.go new file mode 100644 index 00000000..7da11bf7 --- /dev/null +++ b/utils/auto_config/auto_config.go @@ -0,0 +1,91 @@ +package autoconfig + +import ( + "io/ioutil" + "log" + "os" + "path/filepath" + "runtime" + "strings" + + "gopkg.in/yaml.v2" +) + +type ConfigServer struct { + Name string + Host string `yaml:"Host"` + Port int `yaml:"Port"` +} + +func AutoGetAllServerConfig() []*ConfigServer { + var servers []*ConfigServer + etcPath := AutoGetEtcYaml() + err := filepath.Walk(*etcPath+"/server", func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + // Skip if not a file or not a yaml file + if info.IsDir() || filepath.Ext(path) != ".yaml" { + return nil + } + + // Read file + data, err := ioutil.ReadFile(path) + if err != nil { + return err + } + + // Unmarshal the yaml content + var server ConfigServer + err = yaml.Unmarshal(data, &server) + if err != nil { + return err + } + dirs := strings.Split(path, "/") + + server.Name = dirs[len(dirs)-3] + // Add the server to the list + servers = append(servers, &server) + return nil + }) + + if err != nil { + panic("Error: " + err.Error()) + } + return servers +} + +func AutoGetEtcYaml() *string { + var currentFilePath string + var ok bool + + _, currentFilePath, _, ok = runtime.Caller(1) + if !ok { + panic("Error: Unable to get the current file path.") + } + + dirs := strings.Split(currentFilePath, "/") + dirs = dirs[0 : len(dirs)-1] + + for len(dirs) != 0 { + curPath := strings.Join(dirs, "/") + + dirs = dirs[0 : len(dirs)-1] + + // 列出所有 curPath 下的文件夹 + files, err := ioutil.ReadDir(curPath) + if err != nil { + log.Println(err) + continue + } + + // 查找每个文件夹下是否存在 server + for _, file := range files { + if file.Name() == "server" { + return &curPath + } + } + } + + return nil +} diff --git a/utils/auto_config/auto_config_test.go b/utils/auto_config/auto_config_test.go new file mode 100644 index 00000000..7f118a8a --- /dev/null +++ b/utils/auto_config/auto_config_test.go @@ -0,0 +1,7 @@ +package autoconfig + +import "testing" + +func TestAutoConfig(t *testing.T) { + AutoGetEtcYaml() +}