Skip to content
Snippets Groups Projects
Select Git revision
  • 17bc33914bcc98ba3c6b426fd1c49587a25c0597
  • openEuler-1.0-LTS default protected
  • openEuler-22.09
  • OLK-5.10
  • openEuler-22.03-LTS
  • openEuler-22.03-LTS-Ascend
  • master
  • openEuler-22.03-LTS-LoongArch-NW
  • openEuler-22.09-HCK
  • openEuler-20.03-LTS-SP3
  • openEuler-21.09
  • openEuler-21.03
  • openEuler-20.09
  • 4.19.90-2210.5.0
  • 5.10.0-123.0.0
  • 5.10.0-60.63.0
  • 5.10.0-60.62.0
  • 4.19.90-2210.4.0
  • 5.10.0-121.0.0
  • 5.10.0-60.61.0
  • 4.19.90-2210.3.0
  • 5.10.0-60.60.0
  • 5.10.0-120.0.0
  • 5.10.0-60.59.0
  • 5.10.0-119.0.0
  • 4.19.90-2210.2.0
  • 4.19.90-2210.1.0
  • 5.10.0-118.0.0
  • 5.10.0-106.19.0
  • 5.10.0-60.58.0
  • 4.19.90-2209.6.0
  • 5.10.0-106.18.0
  • 5.10.0-106.17.0
33 results

check.c

Blame
  • config.go 2.89 KiB
    /******************************************************
    # DESC    : env var & configure
    # AUTHOR  : Alex Stocks
    # VERSION : 1.0
    # LICENCE : Apache License 2.0
    # EMAIL   : alexstocks@foxmail.com
    # MOD     : 2016-07-21 16:41
    # FILE    : config.go
    ******************************************************/
    
    package main
    
    import (
    	"fmt"
    	"io/ioutil"
    	"os"
    	"path"
    	"time"
    
    	"github.com/AlexStocks/goext/log"
    
    	log "github.com/AlexStocks/log4go"
    
    	jerrors "github.com/juju/errors"
    
    	"github.com/dubbo/dubbo-go/registry"
    	yaml "gopkg.in/yaml.v2"
    )
    
    const (
    	APP_CONF_FILE     string = "APP_CONF_FILE"
    	APP_LOG_CONF_FILE string = "APP_LOG_CONF_FILE"
    )
    
    var (
    	conf *ServerConfig
    )
    
    type (
    	ServerConfig struct {
    		// pprof
    		Pprof_Enabled bool `default:"false" yaml:"pprof_enabled"  json:"pprof_enabled,omitempty"`
    		Pprof_Port    int  `default:"10086"  yaml:"pprof_port" json:"pprof_port,omitempty"`
    
    		// transport & registry
    		Transport  string `default:"http"  yaml:"transport" json:"transport,omitempty"`
    		NetTimeout string `default:"100ms"  yaml:"net_timeout" json:"net_timeout,omitempty"` // in ms
    		netTimeout time.Duration
    		// application
    		Application_Config registry.ApplicationConfig `yaml:"application_config" json:"application_config,omitempty"`
    		// Registry_Address  string `default:"192.168.35.3:2181"`
    		Registry_Config registry.RegistryConfig  `yaml:"registry_config" json:"registry_config,omitempty"`
    		Service_List    []registry.ServiceConfig `yaml:"service_list" json:"service_list,omitempty"`
    		Server_List     []registry.ServerConfig  `yaml:"server_list" json:"server_list,omitempty"`
    	}
    )
    
    func initServerConf() *ServerConfig {
    	var (
    		err      error
    		confFile string
    	)
    
    	confFile = os.Getenv(APP_CONF_FILE)
    	if confFile == "" {
    		panic(fmt.Sprintf("application configure file name is nil"))
    		return nil
    	}
    	if path.Ext(confFile) != ".yml" {
    		panic(fmt.Sprintf("application configure file name{%v} suffix must be .yml", confFile))
    		return nil
    	}
    
    	conf = &ServerConfig{}
    	confFileStream, err := ioutil.ReadFile(confFile)
    	if err != nil {
    		panic(fmt.Sprintf("ioutil.ReadFile(file:%s) = error:%s", confFile, jerrors.ErrorStack(err)))
    		return nil
    	}
    	err = yaml.Unmarshal(confFileStream, conf)
    	if err != nil {
    		panic(fmt.Sprintf("yaml.Unmarshal() = error:%s", jerrors.ErrorStack(err)))
    		return nil
    	}
    	if conf.netTimeout, err = time.ParseDuration(conf.NetTimeout); err != nil {
    		panic(fmt.Sprintf("time.ParseDuration(NetTimeout:%#v) = error:%s", conf.NetTimeout, err))
    		return nil
    	}
    
    	gxlog.CInfo("config{%#v}\n", conf)
    
    	return conf
    }
    
    func configInit() error {
    	var (
    		confFile string
    	)
    
    	initServerConf()
    
    	confFile = os.Getenv(APP_LOG_CONF_FILE)
    	if confFile == "" {
    		panic(fmt.Sprintf("log configure file name is nil"))
    		return nil
    	}
    	if path.Ext(confFile) != ".xml" {
    		panic(fmt.Sprintf("log configure file name{%v} suffix must be .xml", confFile))
    		return nil
    	}
    
    	log.LoadConfiguration(confFile)
    
    	return nil
    }