finish auto_config

This commit is contained in:
eson 2023-07-26 00:19:21 +08:00
parent 38375ffdda
commit 852c5e4a94
18 changed files with 114 additions and 16 deletions

View File

@ -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:

View File

@ -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

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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"

View File

@ -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:

View File

@ -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
}

View File

@ -0,0 +1,7 @@
package autoconfig
import "testing"
func TestAutoConfig(t *testing.T) {
AutoGetEtcYaml()
}