No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.go 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package configure
  2. import (
  3. "time"
  4. "git.x2erp.com/qdy/go-base/config"
  5. )
  6. // AuthType 认证类型
  7. type AuthType string
  8. const (
  9. // AuthTypeBasic Basic认证
  10. AuthTypeBasic AuthType = "basic"
  11. // AuthTypeToken Token认证
  12. AuthTypeToken AuthType = "token"
  13. )
  14. // ClientConfig 客户端配置
  15. type ClientConfig struct {
  16. // BaseURL 配置中心基础URL
  17. BaseURL string `json:"baseURL" yaml:"baseURL"`
  18. // AuthType 认证类型
  19. AuthType AuthType `json:"authType" yaml:"authType"`
  20. // Basic认证配置
  21. Username string `json:"username,omitempty" yaml:"username,omitempty"`
  22. Password string `json:"password,omitempty" yaml:"password,omitempty"`
  23. // Token认证配置
  24. Token string `json:"token,omitempty" yaml:"token,omitempty"`
  25. // HTTP配置
  26. HTTPTimeout time.Duration `json:"httpTimeout,omitempty" yaml:"httpTimeout,omitempty"`
  27. Insecure bool `json:"insecure,omitempty" yaml:"insecure,omitempty"`
  28. }
  29. // DefaultConfig 返回默认配置
  30. // 从全局配置中获取ConfigureConfig的URL和Token
  31. func DefaultConfig() ClientConfig {
  32. cfg := config.GetConfigureConfig()
  33. return ClientConfig{
  34. BaseURL: cfg.Url,
  35. AuthType: AuthTypeToken,
  36. Token: cfg.Token,
  37. HTTPTimeout: 30 * time.Second,
  38. Insecure: false,
  39. }
  40. }
  41. // Validate 验证配置
  42. func (c *ClientConfig) Validate() error {
  43. if c.BaseURL == "" {
  44. return ErrConfigInvalidURL
  45. }
  46. switch c.AuthType {
  47. case AuthTypeBasic:
  48. if c.Username == "" || c.Password == "" {
  49. return ErrConfigInvalidAuth
  50. }
  51. case AuthTypeToken:
  52. if c.Token == "" {
  53. return ErrConfigInvalidAuth
  54. }
  55. default:
  56. return ErrConfigInvalidAuthType
  57. }
  58. return nil
  59. }