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.

70 lines
1.6 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 (
"fmt"
"gopkg.in/ini.v1"
"log"
"os"
"path/filepath"
)
var Config = &App{
MaxLimit: 100,
MinLimit: 1,
DefaultLimit: 15,
}
type App struct {
MaxLimit int `ini:"maxLimit"`
MinLimit int `ini:"minLimit"`
DefaultLimit int `ini:"defaultLimit"`
Debug bool `ini:"debug"`
Server Server `ini:"server"`
Qr Qr `ini:"qr"`
Image Image `ini:"image"`
}
type IniConfig struct {
ConfigPath string
ConfigName string
RunModelName string
RunModel string
RunModelErrAllow bool // 允许不使用拓展配置文件
}
func InitConfig(config *IniConfig) {
if config.ConfigName == "" {
config.ConfigName = "app.ini"
}
// 读取配置文件
load, err := ini.Load(filepath.Join(config.ConfigPath, config.ConfigName))
if err != nil {
log.Fatal("配置文件读取错误err:", err)
return
}
// 环境配置
if config.RunModel == "" {
config.RunModel = os.Getenv("RunModel")
if config.RunModel == "" {
config.RunModel = load.Section("").Key("defaultModel").String()
if config.RunModel == "" {
config.RunModel = "test"
}
}
}
if config.RunModelName == "" {
config.RunModelName = fmt.Sprintf("app.%s.ini", config.RunModel)
}
if err := load.Append(filepath.Join(config.ConfigPath, config.RunModelName)); err != nil {
if config.RunModelErrAllow {
log.Println("环境配置文件读取错误err:", err)
} else {
log.Fatal("环境配置文件读取错误err:", err)
}
}
// 映射配置
if err := load.MapTo(Config); err != nil {
log.Fatal("配置文件映射错误err:", err)
return
}
}