diff options
-rw-r--r-- | msb2pilot/src/msb2pilot/consul/controller.go | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/msb2pilot/src/msb2pilot/consul/controller.go b/msb2pilot/src/msb2pilot/consul/controller.go index 3ea34ec..ca57bd2 100644 --- a/msb2pilot/src/msb2pilot/consul/controller.go +++ b/msb2pilot/src/msb2pilot/consul/controller.go @@ -13,15 +13,28 @@ package consul import ( "msb2pilot/log" + "msb2pilot/util" + "path/filepath" "github.com/hashicorp/consul/api" ) var client *api.Client +var consulAddress string + +var ( + cfgFilePath = filepath.Join(util.GetCfgPath(), "consul.yml") +) + +const ( + defaultAddress = "http://localhost:8500" +) func init() { + consulAddress = getConsulAddress(cfgFilePath) + conf := api.DefaultConfig() - conf.Address = "http://localhost:8500" + conf.Address = consulAddress var err error client, err = api.NewClient(conf) @@ -29,3 +42,32 @@ func init() { log.Log.Error("failed to init consul client", err) } } + +func getConsulAddress(path string) string { + cfg, err := loadCfgInfo(path) + if err != nil { + log.Log.Error("load consul config info error", err) + return defaultAddress + } else { + if addr, exist := cfg["address"]; exist { + return addr.(string) + } else { + return defaultAddress + } + } +} + +func loadCfgInfo(path string) (map[interface{}]interface{}, error) { + log.Log.Informational("consul config path is:" + path) + cfg, err := util.Read(path) + if err != nil { + return nil, err + } + + result := make(map[interface{}]interface{}) + err = util.UnmarshalYaml(cfg, result) + if err != nil { + return nil, err + } + return result, nil +} |