diff --git a/.gitignore b/.gitignore index 0b744481fbfd1ed0c62daa19beccbe84a6acb78d..39f0947ba1d78d395301e31baf8b9106c2b8dc06 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,6 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out -vendor \ No newline at end of file +vendor +go.mod +go.sum \ No newline at end of file diff --git a/configcenter/center.go b/configcenter/center.go new file mode 100644 index 0000000000000000000000000000000000000000..640fd0480de0679e8572830680ea9d9680ca8b85 --- /dev/null +++ b/configcenter/center.go @@ -0,0 +1,147 @@ +package configcenter + +import ( + "errors" + "gitee.com/kelvins-io/common/env" + "github.com/tidwall/gjson" + "log" + "strings" +) + +type ConfigCenter struct { + parse gjson.Result + serverName string + configName string +} + +const ( + DefaultGlobalPath = "/usr/local/etc/global-conf" + DefaultGlobalFileName = "/usr/local/etc/global-conf/config.json" + DefaultConfigRootPath = "/usr/local/etc" + + DefaultCertPemPath = "certs/server.pem" + DefaultCertKeyPath = "certs/server-key.pem" + DefaultCertPemFileName = "server.pem" + DefaultCertKeyFileName = "server-key.pem" + + ServerName = "server_name" + ServerPort = "server_port" + ConfigRootPath = "config_root_path" + CertPemPath = "cert_pem_path" + CertKeyPath = "cert_key_path" + CertServerName = "cert_server_name" +) + +func NewConfigCenter(serverName string) *ConfigCenter { + fileRead := NewFileRead() + jsonByte, err := fileRead.Read(DefaultGlobalFileName) + if err != nil { + log.Fatalf("fileRead.Read err: %v", err) + } + + mode, err := env.GetMode() + if err != nil { + log.Fatalf("env.GetMode err: %v", err) + } + + return &ConfigCenter{ + parse: gjson.Parse(string(jsonByte[:])), + serverName: serverName, + configName: mode + "/" + serverName, + } +} + +// 获取服务名称 +func (c *ConfigCenter) GetServerName() (string, error) { + value := c.parse.Get(c.getPath(ServerName)) + if !value.Exists() { + return ``, errors.New(c.serverName + " server_name not exist") + } + + return value.String(), nil +} + +// 获取服务端口 +func (c *ConfigCenter) GetServerPort() (uint64, error) { + value := c.parse.Get(c.getPath(ServerPort)) + if !value.Exists() { + return 0, errors.New(c.serverName + " server_port not exist") + } + + return value.Uint(), nil +} + +// 获取服务配置目录 +func (c *ConfigCenter) GetServerConfigPath() (string, error) { + value := c.parse.Get(c.getPath(ConfigRootPath)) + if !value.Exists() { + return ``, errors.New(c.serverName + " config_root_path not exist") + } + + return strings.Join([]string{value.String(), c.configName}, "/"), nil +} + +func (c *ConfigCenter) MustGetServerConfigPath() string { + var paths []string + value := c.parse.Get(c.getPath(ConfigRootPath)) + + if !value.Exists() { + paths = []string{DefaultConfigRootPath, c.configName} + } else { + paths = []string{value.String(), c.configName} + } + + return strings.Join(paths, "/") +} + +// 获取证书服务名称 +func (c *ConfigCenter) GetCertServerName() (string, error) { + value := c.parse.Get(c.getPath(CertServerName)) + if !value.Exists() { + return "", errors.New(c.serverName + " cert_server_name not exist") + } + + return value.String(), nil +} + +// 获取证书 .pem 路径 +func (c *ConfigCenter) GetCertPemPath() (string, error) { + value := c.parse.Get(c.getPath(CertPemPath)) + if !value.Exists() { + return ``, errors.New(c.serverName + " cert_pem_path not exist") + } + + return value.String(), nil +} + +func (c *ConfigCenter) MustGetCertPemPath() string { + value := c.parse.Get(c.getPath(CertPemPath)) + if !value.Exists() { + return DefaultCertPemPath + } + + return value.String() +} + +// 获取证书 .key 路径 +func (c *ConfigCenter) GetCertKeyPath() (string, error) { + value := c.parse.Get(c.getPath(CertKeyPath)) + if !value.Exists() { + return ``, errors.New(c.serverName + " cert_key_path not exist") + } else { + return value.String(), nil + } +} + +func (c *ConfigCenter) MustGetCertKeyPath() string { + value := c.parse.Get(c.getPath(CertKeyPath)) + if !value.Exists() { + return DefaultCertKeyPath + } + + return value.String() +} + +func (c *ConfigCenter) getPath(key string) string { + return strings.Join([]string{c.serverName, key}, ".") +} diff --git a/configcenter/center2.go b/configcenter/center2.go new file mode 100644 index 0000000000000000000000000000000000000000..e602ceae4f4739eba48c05c51d7afe8ac375aba7 --- /dev/null +++ b/configcenter/center2.go @@ -0,0 +1,36 @@ +package configcenter + +import ( + "fmt" + "strings" +) + +type ConfigCenterV2 struct { + serverName string +} + +func NewConfigCenterV2(serverName string) *ConfigCenterV2 { + return &ConfigCenterV2{ + serverName: serverName, + } +} + +// 获取证书 .pem 路径 +func (c *ConfigCenterV2) GetCertPemPath() (string, error) { + return c.getPath(DefaultCertPemFileName) +} + +// 获取证书 .key 路径 +func (c *ConfigCenterV2) GetCertKeyPath() (string, error) { + return c.getPath(DefaultCertKeyFileName) +} + +func (c *ConfigCenterV2) getPath(key string) (string, error) { + names := strings.Split(c.serverName, "-") + if len(names) < 1 { + return "", fmt.Errorf("ConfigCenterV2.getPath is empty.") + } + serverTag := names[0] + "-cert" + + return strings.Join([]string{DefaultGlobalPath, "certs", serverTag, key}, "/"), nil +} diff --git a/configcenter/file.go b/configcenter/file.go new file mode 100644 index 0000000000000000000000000000000000000000..edb60892a9fad117732cf1bf8b6c07e32920feb0 --- /dev/null +++ b/configcenter/file.go @@ -0,0 +1,20 @@ +package configcenter + +import ( + "io/ioutil" +) + +type fileRead struct{} + +func NewFileRead() *fileRead { + return &fileRead{} +} + +func (f *fileRead) Read(fileName string) ([]byte, error) { + data, err := ioutil.ReadFile(fileName) + if err != nil { + return nil, err + } + + return data, nil +} diff --git a/env/env.go b/env/env.go index 9dd56a4792ee484cb086eed0559fcfb7a05d1026..b750f8b956725aeae77da5e5986f759b7384e8e1 100644 --- a/env/env.go +++ b/env/env.go @@ -18,7 +18,7 @@ const ( func GetMode() (env string, err error) { env = strings.ToLower(os.Getenv(EnvName)) if env == "" { - err = errors.New("Can not find ENV '" + EnvName + "'") + err = errors.New("can not find env " + EnvName ) } return env, err