diff options
author | Lvbo163 <lv.bo163@zte.com.cn> | 2018-08-02 09:03:25 +0800 |
---|---|---|
committer | Lvbo163 <lv.bo163@zte.com.cn> | 2018-08-02 09:03:25 +0800 |
commit | 294affdfd2270e4479c9861890cd33d69920dd37 (patch) | |
tree | 8162d59f4c174f611d48264502e0e78bee3c4a11 /msb2pilot | |
parent | 952453f8ce9077e9abe0f96ab87e2d073e806664 (diff) |
support setting consul from config file
Issue-ID: MSB-244
Change-Id: I255a737bafd26245f3c484fcbead84b8f8e8b2d6
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
Diffstat (limited to 'msb2pilot')
-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 +} |