You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.4 KiB
71 lines
1.4 KiB
package config
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"epur-pay/model"
|
|
"fmt"
|
|
"gopkg.in/ini.v1"
|
|
)
|
|
|
|
type Config struct {
|
|
Common struct {
|
|
RunMode string `yaml:"RunMode"`
|
|
LogFilePath string `yaml:"LogFilePath"` //日志根路径
|
|
Domain string `yaml:"Domain"`
|
|
ResourcePath string `yaml:"ResourcePath"`
|
|
} `yaml:"Common"`
|
|
ApiServer model.ListenServer `yaml:"ApiServer"`
|
|
Rds model.Rds `yaml:"Rds"`
|
|
Redis model.Redis `yaml:"Redis"`
|
|
}
|
|
|
|
var Cf = Config{}
|
|
|
|
func New(Name string) {
|
|
|
|
var (
|
|
cfg *ini.File
|
|
err error
|
|
configPath string
|
|
)
|
|
|
|
if Cf.Common.RunMode == "debug" {
|
|
configPath = fmt.Sprintf("config/%s_test.ini", Name)
|
|
} else {
|
|
configPath = fmt.Sprintf("config/%s.ini", Name)
|
|
}
|
|
|
|
if cfg, err = ini.Load(configPath); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
mapTo("Common", &Cf.Common, cfg)
|
|
mapTo("ApiServer", &Cf.ApiServer, cfg)
|
|
mapTo("Rds", &Cf.Rds, cfg)
|
|
mapTo("Redis", &Cf.Redis, cfg)
|
|
|
|
fmt.Println("Cf -> config load success.")
|
|
}
|
|
|
|
func (conf *Config) String() string {
|
|
b, err := json.Marshal(*conf)
|
|
if err != nil {
|
|
return fmt.Sprintf("%+v", *conf)
|
|
}
|
|
var out bytes.Buffer
|
|
err = json.Indent(&out, b, "", " ")
|
|
if err != nil {
|
|
return fmt.Sprintf("%+v", *conf)
|
|
}
|
|
return out.String()
|
|
}
|
|
|
|
// mapTo map section
|
|
func mapTo(section string, v interface{}, cfg *ini.File) {
|
|
err := cfg.Section(section).MapTo(v)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|