jytest/config/config.go

66 lines
1.2 KiB
Go

package config
import (
"os"
"gopkg.in/yaml.v3"
)
// Config 全局配置
type Config struct {
Server ServerConfig `yaml:"server"`
API APIConfig `yaml:"api"`
Store StoreConfig `yaml:"store"`
TestProduct []TestDataItem `yaml:"test_product"`
TestModel []TestDataItem `yaml:"test_model"`
}
// TestDataItem 测试数据项
type TestDataItem struct {
Name string `yaml:"name" json:"name"`
URL string `yaml:"url" json:"url"`
}
// ServerConfig 服务器配置
type ServerConfig struct {
Port int `yaml:"port"`
}
// APIConfig API配置
type APIConfig struct {
AccessKey string `yaml:"access_key"`
SecretKey string `yaml:"secret_key"`
Host string `yaml:"host"`
}
// StoreConfig 存储配置
type StoreConfig struct {
DataFile string `yaml:"data_file"`
}
// Load 加载配置文件
func Load(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, err
}
// 设置默认值
if cfg.Server.Port == 0 {
cfg.Server.Port = 8080
}
if cfg.API.Host == "" {
cfg.API.Host = "https://jimeng.jianying.com"
}
if cfg.Store.DataFile == "" {
cfg.Store.DataFile = "data/tasks.json"
}
return &cfg, nil
}