2021-09-07 13:40:44 +08:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Gateway struct {
|
2021-09-09 11:46:04 +08:00
|
|
|
Routes []Route `yaml:"routes"`
|
2021-09-07 13:40:44 +08:00
|
|
|
} `yaml:"gateway"`
|
|
|
|
}
|
|
|
|
|
2021-09-09 11:46:04 +08:00
|
|
|
type Route struct {
|
|
|
|
Path string `yaml:"path"`
|
|
|
|
Service string `yaml:"service"`
|
|
|
|
Enable bool `yaml:"enable"`
|
|
|
|
Lb Lb `yaml:"lb"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Lb struct {
|
|
|
|
Host string `yaml:"host"`
|
|
|
|
Port int `yaml:"port"`
|
|
|
|
}
|
|
|
|
|
2021-09-07 13:40:44 +08:00
|
|
|
func ConfigChanged(data string) {
|
|
|
|
// 从配置文件解析为对象
|
|
|
|
config := Config{}
|
|
|
|
err := yaml.Unmarshal([]byte(data), &config)
|
|
|
|
if err != nil {
|
2021-09-07 17:12:29 +08:00
|
|
|
Log.Error("解析配置文本失败: %v\n", err.Error())
|
2021-09-07 13:40:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
rs := config.Gateway.Routes
|
2021-09-09 11:46:04 +08:00
|
|
|
var sma []serviceItem
|
2021-09-07 13:40:44 +08:00
|
|
|
for _, r := range rs {
|
2021-09-09 11:46:04 +08:00
|
|
|
item := NewServiceMapItem(r.Path, r.Service, r.Enable, r.Lb)
|
2021-09-07 13:40:44 +08:00
|
|
|
sma = append(sma, item)
|
|
|
|
}
|
|
|
|
ServiceMap = sma
|
2021-09-07 17:12:29 +08:00
|
|
|
Log.Info("配置文件解析完毕")
|
2021-09-07 13:40:44 +08:00
|
|
|
}
|