fusenapi/proxyserver/get_zero_Info.go

137 lines
2.7 KiB
Go
Raw Normal View History

2023-07-11 05:07:44 +00:00
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
)
2023-07-17 11:43:43 +00:00
// Config 结构体用于解析yaml配置文件
2023-07-11 05:07:44 +00:00
type Config struct {
Host string `yaml:"Host"`
Port int `yaml:"Port"`
}
2023-07-17 11:43:43 +00:00
// Result 结构体用于存储解析结果
2023-07-11 05:07:44 +00:00
type Result struct {
FolderName string
Host string
Port int
PrefixRoute map[string]bool
}
2023-07-17 11:43:43 +00:00
// GetZeroInfo 遍历指定目录,并解析相关信息
2023-07-11 05:07:44 +00:00
func GetZeroInfo(rootDir string) (results []*Result) {
entries, err := ioutil.ReadDir(rootDir)
if err != nil {
log.Fatal(err)
}
for _, entry := range entries {
2023-07-17 11:43:43 +00:00
// 只处理目录类型
2023-07-11 05:07:44 +00:00
if entry.IsDir() {
result, err := findFoldersAndExtractInfo(rootDir, entry)
if err != nil {
log.Fatal(err)
}
results = append(results, result)
}
}
return
}
2023-07-17 11:43:43 +00:00
// findFoldersAndExtractInfo 查找目录并提取信息
2023-07-11 05:07:44 +00:00
func findFoldersAndExtractInfo(rootDir string, entry os.FileInfo) (*Result, error) {
var result *Result
folderName := entry.Name()
path := filepath.Join(rootDir, folderName)
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
2023-07-17 11:43:43 +00:00
// 跳过非目录类型
2023-07-11 05:07:44 +00:00
if !info.IsDir() {
return nil
}
relPath, err := filepath.Rel(path, path)
if err != nil {
return err
}
2023-07-17 11:43:43 +00:00
// 跳过非当前目录的子目录
2023-07-11 05:07:44 +00:00
if strings.Contains(relPath, string(os.PathSeparator)) {
return filepath.SkipDir
}
2023-07-17 11:43:43 +00:00
// 读取配置文件
2023-07-11 05:07:44 +00:00
configPath := filepath.Join(path, "etc", folderName+".yaml")
routesPath := filepath.Join(path, "internal", "handler", "routes.go")
configContent, err := ioutil.ReadFile(configPath)
if err != nil {
return err
}
var config Config
err = yaml.Unmarshal(configContent, &config)
if err != nil {
return err
}
2023-07-17 11:43:43 +00:00
// 读取路由文件
2023-07-11 05:07:44 +00:00
routesContent, err := ioutil.ReadFile(routesPath)
if err != nil {
return err
}
PrefixRoute := extractPrefixRouteValues(string(routesContent))
2023-07-17 11:43:43 +00:00
// 构建结果
2023-07-11 05:07:44 +00:00
result = &Result{
FolderName: folderName,
Host: config.Host,
Port: config.Port,
PrefixRoute: PrefixRoute,
}
return filepath.SkipDir
})
if err != nil {
return nil, err
}
return result, nil
}
2023-07-17 11:43:43 +00:00
// extractPrefixRouteValues 提取路由前缀
2023-07-11 05:07:44 +00:00
func extractPrefixRouteValues(content string) map[string]bool {
lines := strings.Split(content, "\n")
var prefixPath map[string]bool = make(map[string]bool)
for _, line := range lines {
2023-07-17 11:43:43 +00:00
// 查找包含 "Path:" 的行
2023-07-11 05:07:44 +00:00
if strings.Contains(line, "Path:") {
path := strings.TrimSpace(strings.TrimPrefix(line, "Path:"))
2023-07-12 06:11:04 +00:00
paths := strings.Split(strings.Trim(path, `"`), "/")
path1 := "/" + paths[1] + "/" + paths[2]
2023-07-11 05:07:44 +00:00
if _, ok := prefixPath[path1]; !ok {
prefixPath[path1] = true
}
}
}
return prefixPath
}