github.com/XiaoMi/Gaea@v1.2.5/models/proxy.go (about)

     1  // Copyright 2019 The Gaea Authors. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package models
    16  
    17  import (
    18  	"strings"
    19  
    20  	"github.com/go-ini/ini"
    21  )
    22  
    23  const (
    24  	defaultGaeaCluster = "gaea"
    25  )
    26  
    27  // Proxy means proxy structure of proxy config
    28  type Proxy struct {
    29  	// config type
    30  	ConfigType string `ini:"config_type"`
    31  
    32  	// 文件配置类型内容
    33  	FileConfigPath string `ini:"file_config_path"`
    34  
    35  	// etcd 相关配置
    36  	CoordinatorAddr string `ini:"coordinator_addr"`
    37  	CoordinatorRoot string `ini:"coordinator_root"`
    38  	UserName        string `ini:"username"`
    39  	Password        string `ini:"password"`
    40  
    41  	// 服务相关信息
    42  	Environ string `ini:"environ"`
    43  	Service string `ini:"service_name"`
    44  	Cluster string `ini:"cluster_name"`
    45  
    46  	LogPath     string `ini:"log_path"`
    47  	LogLevel    string `ini:"log_level"`
    48  	LogFileName string `ini:"log_filename"`
    49  	LogOutput   string `ini:"log_output"`
    50  
    51  	ProtoType      string `ini:"proto_type"`
    52  	ProxyAddr      string `ini:"proxy_addr"`
    53  	AdminAddr      string `ini:"admin_addr"`
    54  	AdminUser      string `ini:"admin_user"`
    55  	AdminPassword  string `ini:"admin_password"`
    56  	SlowSQLTime    int64  `ini:"slow_sql_time"`
    57  	SessionTimeout int    `ini:"session_timeout"`
    58  
    59  	// 监控配置
    60  	StatsEnabled  string `ini:"stats_enabled"`  // set true to enable stats
    61  	StatsInterval int    `ini:"stats_interval"` // set stats interval of connect pool
    62  
    63  	EncryptKey string `ini:"encrypt_key"`
    64  
    65  	ServerVersion string `ini:"server_version"`
    66  	AuthPlugin    string `ini:"auth_plugin"`
    67  }
    68  
    69  // ParseProxyConfigFromFile parser proxy config from file
    70  func ParseProxyConfigFromFile(cfgFile string) (*Proxy, error) {
    71  	cfg, err := ini.Load(cfgFile)
    72  
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	var proxyConfig = &Proxy{}
    78  	err = cfg.MapTo(proxyConfig)
    79  	// default config type: etcd
    80  	if proxyConfig.ConfigType == "" {
    81  		proxyConfig.ConfigType = ConfigEtcd
    82  	}
    83  	if proxyConfig.Cluster == "" && proxyConfig.CoordinatorRoot == "" {
    84  		proxyConfig.Cluster = defaultGaeaCluster
    85  	} else if proxyConfig.Cluster == "" && proxyConfig.CoordinatorRoot != "" {
    86  		proxyConfig.Cluster = strings.TrimPrefix(proxyConfig.CoordinatorRoot, "/")
    87  	} else if proxyConfig.Cluster != "" {
    88  		proxyConfig.CoordinatorRoot = "/" + proxyConfig.Cluster
    89  	}
    90  	return proxyConfig, err
    91  }
    92  
    93  // Verify verify proxy config
    94  func (p *Proxy) Verify() error {
    95  	return nil
    96  }
    97  
    98  // ProxyInfo for report proxy information
    99  type ProxyInfo struct {
   100  	Token     string `json:"token"`
   101  	StartTime string `json:"start_time"`
   102  
   103  	IP        string `json:"ip"`
   104  	ProtoType string `json:"proto_type"`
   105  	ProxyPort string `json:"proxy_port"`
   106  	AdminPort string `json:"admin_port"`
   107  
   108  	Pid int    `json:"pid"`
   109  	Pwd string `json:"pwd"`
   110  	Sys string `json:"sys"`
   111  }
   112  
   113  // Encode encode proxy info
   114  func (p *ProxyInfo) Encode() []byte {
   115  	return JSONEncode(p)
   116  }