2022-01-24 23:09:26 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-27 12:04:46 +08:00
|
|
|
"fmt"
|
2022-01-24 23:09:26 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
2022-01-26 22:23:54 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gcmd"
|
2022-01-24 23:09:26 +08:00
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
|
|
|
)
|
|
|
|
|
2022-01-26 20:51:17 +08:00
|
|
|
const (
|
2022-01-26 22:23:54 +08:00
|
|
|
separator = "/"
|
2022-01-26 20:51:17 +08:00
|
|
|
)
|
|
|
|
|
2022-01-26 22:23:54 +08:00
|
|
|
// NewServiceWithName creates and returns service from `name`.
|
|
|
|
func NewServiceWithName(name string) (s *Service) {
|
|
|
|
s = &Service{
|
|
|
|
Name: name,
|
|
|
|
Metadata: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
s.autoFillDefaultAttributes()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServiceWithKV creates and returns service from `key` and `value`.
|
|
|
|
func NewServiceWithKV(key, value []byte) (s *Service, err error) {
|
2022-01-26 23:43:48 +08:00
|
|
|
array := gstr.Split(gstr.Trim(string(key), separator), separator)
|
2022-01-26 20:51:17 +08:00
|
|
|
if len(array) < 6 {
|
|
|
|
return nil, gerror.NewCodef(gcode.CodeInvalidParameter, `invalid service key "%s"`, key)
|
|
|
|
}
|
|
|
|
s = &Service{
|
|
|
|
Prefix: array[0],
|
|
|
|
Deployment: array[1],
|
|
|
|
Namespace: array[2],
|
|
|
|
Name: array[3],
|
|
|
|
Version: array[4],
|
2022-01-26 22:23:54 +08:00
|
|
|
Endpoints: gstr.Split(array[5], ","),
|
2022-01-26 20:51:17 +08:00
|
|
|
Metadata: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
s.autoFillDefaultAttributes()
|
|
|
|
if len(value) > 0 {
|
|
|
|
if err = gjson.Unmarshal(value, &s.Metadata); err != nil {
|
|
|
|
return nil, gerror.WrapCodef(gcode.CodeInvalidParameter, err, `invalid service value "%s"`, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2022-01-24 23:09:26 +08:00
|
|
|
// Key formats the service information and returns the Service as registering key.
|
|
|
|
func (s *Service) Key() string {
|
2022-01-26 22:23:54 +08:00
|
|
|
serviceNameUnique := s.KeyWithoutEndpoints()
|
|
|
|
serviceNameUnique += separator + gstr.Join(s.Endpoints, ",")
|
|
|
|
return serviceNameUnique
|
|
|
|
}
|
|
|
|
|
2022-01-27 12:04:46 +08:00
|
|
|
// KeyWithSchema formats the service information and returns the Service as dialing target key.
|
|
|
|
func (s *Service) KeyWithSchema() string {
|
|
|
|
return fmt.Sprintf(`%s://%s`, Schema, s.Key())
|
|
|
|
}
|
|
|
|
|
2022-01-26 22:23:54 +08:00
|
|
|
// KeyWithoutEndpoints formats the service information and returns a string as unique name of service.
|
|
|
|
func (s *Service) KeyWithoutEndpoints() string {
|
2022-01-26 20:51:17 +08:00
|
|
|
s.autoFillDefaultAttributes()
|
2022-01-26 23:43:48 +08:00
|
|
|
return "/" + gstr.Join([]string{
|
2022-01-24 23:09:26 +08:00
|
|
|
s.Prefix,
|
|
|
|
s.Deployment,
|
|
|
|
s.Namespace,
|
|
|
|
s.Name,
|
|
|
|
s.Version,
|
2022-01-26 22:23:54 +08:00
|
|
|
}, separator)
|
2022-01-24 23:09:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Value() string {
|
|
|
|
b, err := gjson.Marshal(s.Metadata)
|
|
|
|
if err != nil {
|
|
|
|
intlog.Error(context.TODO(), err)
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2022-01-26 20:51:17 +08:00
|
|
|
func (s *Service) autoFillDefaultAttributes() {
|
|
|
|
if s.Prefix == "" {
|
2022-01-26 22:23:54 +08:00
|
|
|
s.Prefix = gcmd.GetOptWithEnv(EnvPrefix, DefaultPrefix).String()
|
2022-01-24 23:09:26 +08:00
|
|
|
}
|
2022-01-26 20:51:17 +08:00
|
|
|
if s.Deployment == "" {
|
2022-01-26 22:23:54 +08:00
|
|
|
s.Deployment = gcmd.GetOptWithEnv(EnvDeployment, DefaultDeployment).String()
|
2022-01-24 23:09:26 +08:00
|
|
|
}
|
2022-01-26 20:51:17 +08:00
|
|
|
if s.Namespace == "" {
|
2022-01-26 22:23:54 +08:00
|
|
|
s.Namespace = gcmd.GetOptWithEnv(EnvNamespace, DefaultNamespace).String()
|
2022-01-26 20:51:17 +08:00
|
|
|
}
|
|
|
|
if s.Version == "" {
|
2022-01-26 22:23:54 +08:00
|
|
|
s.Version = gcmd.GetOptWithEnv(EnvVersion, DefaultVersion).String()
|
|
|
|
}
|
|
|
|
if s.Name == "" {
|
|
|
|
s.Name = gcmd.GetOptWithEnv(EnvName).String()
|
2022-01-24 23:09:26 +08:00
|
|
|
}
|
|
|
|
}
|