gateway/core/config.go
李寻欢 feb62f12b7
All checks were successful
continuous-integration/drone/push Build is passing
网关新增走集群内部网络转发
2021-09-09 11:46:04 +08:00

42 lines
810 B
Go

package core
import (
"gopkg.in/yaml.v3"
)
type Config struct {
Gateway struct {
Routes []Route `yaml:"routes"`
} `yaml:"gateway"`
}
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"`
}
func ConfigChanged(data string) {
// 从配置文件解析为对象
config := Config{}
err := yaml.Unmarshal([]byte(data), &config)
if err != nil {
Log.Error("解析配置文本失败: %v\n", err.Error())
return
}
rs := config.Gateway.Routes
var sma []serviceItem
for _, r := range rs {
item := NewServiceMapItem(r.Path, r.Service, r.Enable, r.Lb)
sma = append(sma, item)
}
ServiceMap = sma
Log.Info("配置文件解析完毕")
}