gitee.com/quant1x/engine@v1.8.4/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"embed"
     5  	"fmt"
     6  	"gitee.com/quant1x/gox/api"
     7  	"gitee.com/quant1x/gox/logger"
     8  	"gitee.com/quant1x/gox/util/homedir"
     9  	"gitee.com/quant1x/pkg/yaml"
    10  	"os"
    11  	"strings"
    12  )
    13  
    14  const (
    15  	// ResourcesPath 资源路径
    16  	ResourcesPath = "resources"
    17  )
    18  
    19  //go:embed resources/*
    20  var resources embed.FS
    21  
    22  // 配置常量
    23  const (
    24  	// 配置文件名
    25  	configFilename = "quant1x.yaml"
    26  )
    27  
    28  var (
    29  	quant1XConfigFilename = "" // 配置文件完整路径
    30  	listConfigFile        = []string{
    31  		"~/." + configFilename,
    32  		"~/.quant1x/" + configFilename,
    33  		"~/runtime/etc/" + configFilename,
    34  	}
    35  )
    36  
    37  // Quant1XConfig Quant1X基础配置
    38  type Quant1XConfig struct {
    39  	BaseDir string           `yaml:"basedir"` // 基础路径
    40  	Data    DataParameter    `yaml:"data"`    // 数据源
    41  	Runtime RuntimeParameter `yaml:"runtime"` // 运行时参数
    42  	Trader  TraderParameter  `yaml:"trader"`  // 预览交易参数
    43  }
    44  
    45  // GetConfigFilename 获取配置文件路径
    46  func GetConfigFilename() string {
    47  	return quant1XConfigFilename
    48  }
    49  
    50  var (
    51  	// GlobalConfig engine配置信息
    52  	GlobalConfig Quant1XConfig
    53  )
    54  
    55  // LoadConfig 加载配置文件
    56  func LoadConfig() (config Quant1XConfig, found bool) {
    57  	for _, v := range listConfigFile {
    58  		filename, err := homedir.Expand(v)
    59  		if err != nil {
    60  			continue
    61  		}
    62  		if !api.FileExist(filename) {
    63  			continue
    64  		}
    65  		err = parseYamlConfig(filename, &config)
    66  		if err != nil {
    67  			logger.Fatalf("%+v", err)
    68  		}
    69  		found = true
    70  		break
    71  	}
    72  	return
    73  }
    74  
    75  // ReadConfig 读取配置文件
    76  func ReadConfig(rootPath string) (config Quant1XConfig) {
    77  	target := GetConfigFilename()
    78  	// 如果文件不存在, 导出默认配置
    79  	if !api.FileExist(target) {
    80  		target = rootPath + "/" + configFilename
    81  		target, _ = homedir.Expand(target)
    82  		filename := fmt.Sprintf("%s/%s", ResourcesPath, configFilename)
    83  		_ = api.Export(resources, filename, target)
    84  	}
    85  	err := parseYamlConfig(target, &config)
    86  	if err != nil {
    87  		logger.Fatalf("%+v", err)
    88  	}
    89  	return
    90  }
    91  
    92  func parseYamlConfig(filename string, config *Quant1XConfig) error {
    93  	if api.FileExist(filename) {
    94  		dataBytes, err := os.ReadFile(filename)
    95  		if err != nil {
    96  			logger.Error(err)
    97  			return err
    98  		}
    99  		err = yaml.Unmarshal(dataBytes, config)
   100  		if err != nil {
   101  			logger.Error(err)
   102  			return err
   103  		}
   104  		config.BaseDir = strings.TrimSpace(config.BaseDir)
   105  		if len(config.BaseDir) > 0 {
   106  			quant1XConfigFilename = filename
   107  		}
   108  	}
   109  	return nil
   110  }