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.

80 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package config
import (
"gopkg.in/ini.v1"
"live/app/lib/asset"
"log"
"os"
)
var Config config
type config struct {
*ini.File
RUNMODE string
WorkPath string
}
func init() {
var err error
Config.WorkPath, err = os.Getwd()
if err != nil {
panic(err)
}
// 配置读取
Config.File, err = ini.Load(Config.WorkPath + "/config/app.ini")
if err != nil {
for _, s := range asset.AssetNames() {
if err := asset.RestoreAsset(Config.WorkPath, s); err != nil {
log.Fatal("释放资源出错", err)
return
}
}
Config.File, err = ini.Load(Config.WorkPath + "/config/app.ini")
if err != nil {
log.Fatal("配置文件读取失败err:", err)
return
}
}
Config.ValueMapper = ExpandValueEnv
Config.RUNMODE = Config.Section("").Key("runmode").String()
if Config.RUNMODE != "" {
Config.Append(Config.WorkPath + "/config/app." + Config.RUNMODE + ".ini")
}
}
func ExpandValueEnv(value string) (realValue string) {
realValue = value
vLen := len(value)
// 3 = ${}
if vLen < 3 {
return
}
// Need start with "${" and end with "}", then return.
if value[0] != '$' || value[1] != '{' || value[vLen-1] != '}' {
return
}
key := ""
defaultV := ""
// value start with "${"
for i := 2; i < vLen; i++ {
if value[i] == '|' && (i+1 < vLen && value[i+1] == '|') {
key = value[2:i]
defaultV = value[i+2 : vLen-1] // other string is default value.
break
} else if value[i] == '}' {
key = value[2:i]
break
}
}
realValue = os.Getenv(key)
if realValue == "" {
realValue = defaultV
}
return
}