Hanlen
11/15/2019 - 10:01 AM

go yaml config

// @copyright (C) ChinaNetCenter Inc. All rights reserved.
// @author linzhix@wangsu.com
// @date 2018/3/31 10:30
// @description
package base

import (
	"fmt"
	"io/ioutil"
	"sync"

	"github.com/go-yaml/yaml"
)

var (
	yamlConfig *YamlConfig
	yamlOnce   sync.Once
)

type Config struct {
	LogLevel           int //log level:1-DEBUG;2-INFO;3-WARN;4-ERROR;5-FATAL;6-PANIC
	LogSizeLimit       int
	LogRotateNum       int
	LogCompress        bool
	IpDetectedInterval int
	N2nDuration        int

	AppsInfoInterval  int
	ReportAppInfoAddr string

	ConcurrentNum  int
	DebugFlag      bool
	SshPort        int
	ProbePort      int
	RegisterAddr   string
	UpdateAddr     string
	SSHN2nAddr     string
	N2nIpRespAddr  string
	UpdateInterval int
}

type YamlConfig struct {
	Config
	configStr string
}

func GetYamlConfig() *YamlConfig {
	yamlOnce.Do(func() {
		yamlConfig = &YamlConfig{}
		yamlConfig.LogLevel = 2
		yamlConfig.LogSizeLimit = 256
		yamlConfig.LogRotateNum = 1
		yamlConfig.LogCompress = true
		yamlConfig.DebugFlag = false
		yamlConfig.SshPort = 8080
		yamlConfig.ProbePort = 28888
		yamlConfig.UpdateInterval = 3600
		yamlConfig.IpDetectedInterval = 3 * 3600
		yamlConfig.N2nDuration = 3600
		yamlConfig.AppsInfoInterval = 1800
		yamlConfig.ReportAppInfoAddr = "http://be01.ecpbox.haplat.net/ecp-box-entry/rest/program/list"
		yamlConfig.RegisterAddr = "http://be01.ecpbox.haplat.net/ecp-box-entry/rest/boxRegister"
		yamlConfig.UpdateAddr = "http://bi01.ecpbox.haplat.net/ecp-box-platform-interface/rest/currentVer"

		yamlConfig.SSHN2nAddr = "http://bi01.ecpbox.haplat.net/ecp-box-entry/rest/config/sshn2n"
		yamlConfig.N2nIpRespAddr = "http://bi01.ecpbox.haplat.net/ecp-box-entry/rest/n2n/ip"
	})
	return yamlConfig
}

func (c *YamlConfig) Init(path string) {
	content, err := ioutil.ReadFile(path)
	if err != nil {
		return
	}

	if c.configStr == string(content[:]) && len(c.configStr) != 0 {
		return
	}
	c.configStr = string(content[:])
	if Verbose {
		fmt.Println("yaml reload :", c.configStr)
	}

	err = yaml.Unmarshal(content, &c.Config)
	if err != nil {
		if Verbose {
			fmt.Println("yaml parse error ", err)
		}
		return
	}
}

func (c *Config) GetLogLevel() int {
	return c.LogLevel
}

func (c *Config) SetLogLevel(level int) {
	c.LogLevel = level
}

func (c *Config) GetLogSizeLimit() int {
	return c.LogSizeLimit
}

func (c *Config) SetLogSizeLimit(size int) {
	c.LogSizeLimit = size
}

func (c *Config) GetLogRotateNum() int {
	return c.LogRotateNum
}

func (c *Config) SetLogRotateNum(num int) {
	c.LogRotateNum = num
}

func (c *Config) GetLogCompress() bool {
	return c.LogCompress
}

func (c *Config) SetLogCompress(flag bool) {
	c.LogCompress = flag
}

func (c *Config) GetDebugFlag() bool {
	return c.DebugFlag
}

func (c *Config) SetDebugFlag(flag bool) {
	c.DebugFlag = flag
}

func (c *Config) GetConcurrentNum() int {
	return c.ConcurrentNum
}

func (c *Config) SetConcurrentNum(num int) {
	c.ConcurrentNum = num
}

func (c *Config) GetProbePort() int {
	return c.ProbePort
}

func (c *Config) SetProbePort(port int) {
	c.ProbePort = port
}

func (c *Config) GetSshPort() int {
	return c.SshPort
}

func (c *Config) SetSshPort(port int) {
	c.SshPort = port
}

func (c *Config) GetRegisterAddr() string {
	return c.RegisterAddr
}

func (c *Config) SetRegisterAddr(addr string) {
	c.RegisterAddr = addr
}

func (c *Config) GetUpdateAddr() string {
	return c.UpdateAddr
}

func (c *Config) SetUpdateAddr(addr string) {
	c.UpdateAddr = addr
}

func (c *Config) GetSshN2nAddr() string {
	return c.SSHN2nAddr
}

func (c *Config) SetSshN2nAddr(addr string) {
	c.SSHN2nAddr = addr
}

func (c *Config) GetN2nIpRespAddr() string {
	return c.N2nIpRespAddr
}

func (c *Config) SetN2nIpRespAddr(addr string) {
	c.N2nIpRespAddr = addr
}

func (c *Config) SetUpdateInterval(interval int) {
	c.UpdateInterval = interval
}

func (c *Config) GetUpdateInterval() int {
	return c.UpdateInterval
}

func (c *Config) SetIpDetectedInterval(interval int) {
	c.IpDetectedInterval = interval
}

func (c *Config) GetIpDetectedInterval() int {
	return c.IpDetectedInterval
}

func (c *Config) SetN2nDuration(interval int) {
	c.N2nDuration = interval
}

func (c *Config) GetN2nDuration() int {
	return c.N2nDuration
}

func (c *Config) SetAppsInfoInterval(interval int) {
	c.AppsInfoInterval = interval
}

func (c *Config) GetAppsInfoInterval() int {
	return c.AppsInfoInterval
}

func (c *Config) SetReportAppInfoAddr(addr string) {
	c.ReportAppInfoAddr = addr
}

func (c *Config) GetReportAppInfoAddr() string {
	return c.ReportAppInfoAddr
}