github.com/GuanceCloud/cliutils@v1.1.21/pprofparser/cfg/cfg.go (about) 1 package cfg 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "gopkg.in/yaml.v2" 9 ) 10 11 const ( 12 DefaultWorkDir = "/usr/local/pprofparser" 13 DefaultCfgName = "conf.yml" 14 DefaultCfgPath = DefaultWorkDir + "/" + DefaultCfgName 15 ) 16 17 const ( 18 EnvLocal = "local" 19 EnvDev = "dev" 20 EnvTest = "test" 21 EnvPre = "pre" 22 EnvProduction = "prod" 23 ) 24 25 var ( 26 Cfg *Config 27 ) 28 29 func Load(file string) error { 30 reader, err := os.Open(file) 31 if err != nil { 32 var wd string 33 if wd, err = os.Getwd(); err == nil { 34 cfgFile := filepath.Join(wd, "cfg", DefaultCfgName) 35 reader, err = os.Open(cfgFile) 36 } 37 if err != nil { 38 return fmt.Errorf("read config file fail: %w", err) 39 } 40 } 41 defer func() { 42 _ = reader.Close() 43 }() 44 45 decoder := yaml.NewDecoder(reader) 46 var cfg Config 47 if err := decoder.Decode(&cfg); err != nil { 48 return fmt.Errorf("decode yaml config file fail: %w", err) 49 } 50 Cfg = &cfg 51 return nil 52 } 53 54 type Config struct { 55 Serv Server `yaml:"server"` 56 Log Log `yaml:"log"` 57 Gin Gin `yaml:"gin"` 58 Oss Oss `yaml:"oss"` 59 Storage Storage `yaml:"storage"` 60 } 61 62 // Server configuration 63 type Server struct { 64 Addr string `yaml:"addr"` 65 Port string `yaml:"port"` 66 } 67 68 // Log log configuration 69 type Log struct { 70 Path string `yaml:"path"` 71 File string `yaml:"file"` 72 Level string `yaml:"level"` 73 } 74 75 // Gin gin configuration 76 type Gin struct { 77 RunMode string `yaml:"run_mode"` 78 Log string `yaml:"log"` 79 ErrorLog string `yaml:"error_log"` 80 } 81 82 // Oss aliyun oss configuration 83 type Oss struct { 84 Host string `yaml:"host"` 85 AccessKey string `yaml:"access_key"` 86 SecretKey string `yaml:"secret_key"` 87 ProfileBucket string `yaml:"profile_bucket"` 88 ProfileDir string `yaml:"profile_dir"` 89 } 90 91 type Disk struct { 92 ProfileDir string `yaml:"profile_dir"` 93 } 94 95 type Storage struct { 96 Disk Disk `yaml:"disk"` 97 Oss Oss `yaml:"oss"` 98 }