34 lines
658 B
Go
34 lines
658 B
Go
package core
|
|
|
|
import (
|
|
"gopkg.in/yaml.v3"
|
|
"log"
|
|
)
|
|
|
|
type Config struct {
|
|
Gateway struct {
|
|
Routes []struct {
|
|
Path string `yaml:"path"`
|
|
Service string `yaml:"service"`
|
|
} `yaml:"routes"`
|
|
} `yaml:"gateway"`
|
|
}
|
|
|
|
func ConfigChanged(data string) {
|
|
// 从配置文件解析为对象
|
|
config := Config{}
|
|
err := yaml.Unmarshal([]byte(data), &config)
|
|
if err != nil {
|
|
log.Printf("解析配置文本失败: %v\n", err.Error())
|
|
return
|
|
}
|
|
rs := config.Gateway.Routes
|
|
var sma []serviceMap
|
|
for _, r := range rs {
|
|
item := NewServiceMapItem(r.Path, r.Service)
|
|
sma = append(sma, item)
|
|
}
|
|
ServiceMap = sma
|
|
log.Println("配置文件解析完毕")
|
|
}
|