| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package configure
-
- import (
- "time"
-
- "git.x2erp.com/qdy/go-base/config"
- )
-
- // AuthType 认证类型
- type AuthType string
-
- const (
- // AuthTypeBasic Basic认证
- AuthTypeBasic AuthType = "basic"
- // AuthTypeToken Token认证
- AuthTypeToken AuthType = "token"
- )
-
- // ClientConfig 客户端配置
- type ClientConfig struct {
- // BaseURL 配置中心基础URL
- BaseURL string `json:"baseURL" yaml:"baseURL"`
-
- // AuthType 认证类型
- AuthType AuthType `json:"authType" yaml:"authType"`
-
- // Basic认证配置
- Username string `json:"username,omitempty" yaml:"username,omitempty"`
- Password string `json:"password,omitempty" yaml:"password,omitempty"`
-
- // Token认证配置
- Token string `json:"token,omitempty" yaml:"token,omitempty"`
-
- // HTTP配置
- HTTPTimeout time.Duration `json:"httpTimeout,omitempty" yaml:"httpTimeout,omitempty"`
- Insecure bool `json:"insecure,omitempty" yaml:"insecure,omitempty"`
- }
-
- // DefaultConfig 返回默认配置
- // 从全局配置中获取ConfigureConfig的URL和Token
- func DefaultConfig() ClientConfig {
- cfg := config.GetConfigureConfig()
- return ClientConfig{
- BaseURL: cfg.Url,
- AuthType: AuthTypeToken,
- Token: cfg.Token,
- HTTPTimeout: 30 * time.Second,
- Insecure: false,
- }
- }
-
- // Validate 验证配置
- func (c *ClientConfig) Validate() error {
- if c.BaseURL == "" {
- return ErrConfigInvalidURL
- }
-
- switch c.AuthType {
- case AuthTypeBasic:
- if c.Username == "" || c.Password == "" {
- return ErrConfigInvalidAuth
- }
- case AuthTypeToken:
- if c.Token == "" {
- return ErrConfigInvalidAuth
- }
- default:
- return ErrConfigInvalidAuthType
- }
-
- return nil
- }
|