1 Star 0 Fork 0

robertzhai / go_common

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
app.go 2.07 KB
一键复制 编辑 原始数据 按行查看 历史
robertzhai 提交于 2023-06-16 10:28 . init
package config
import (
"fmt"
"github.com/go-yaml/yaml"
"io/ioutil"
"os"
)
var AppConfig *AppConfiguration
type DbConf struct {
DefaultMasterDsn string `yaml:"defaultMasterDsn"`
DefaultMasterIdle int `yaml:"defaultMasterIdle"`
DefaultMasterOpen int `yaml:"defaultMasterOpen"`
DefaultSlaveDsn string `yaml:"defaultSlaveDsn"`
DefaultSlaveIdle int `yaml:"defaultSlaveIdle"`
DefaultSlaveOpen int `yaml:"defaultSlaveOpen"`
}
type LogDetail struct {
Level string `yaml:"level"`
File string `yaml:"file"`
BackupNum int `yaml:"backup_num"`
MaxSize int `yaml:"max_size"`
RetainDays int `yaml:"retain_days"`
}
type LogConf struct {
Default LogDetail `yaml:"default"`
Trace LogDetail `yaml:"trace"`
BackupNum int `yaml:"backup_num"`
MaxSize int `yaml:"max_size"`
RetainDays int `yaml:"retain_days"`
}
type KvDbConf struct {
KvMasterDsn string `yaml:"kvMasterDsn"`
KvMasterIdle int `yaml:"kvMasterIdle"`
KvMasterOpen int `yaml:"kvMasterOpen"`
KvSlaveDsn string `yaml:"kvSlaveDsn"`
KvSlaveIdle int `yaml:"kvSlaveIdle"`
KvSlaveOpen int `yaml:"kvSlaveOpen"`
}
type AppConfiguration struct {
// app名称
AppName string `yaml:"appName"`
// listen addr
Listen string `yaml:"listen"`
LogConf LogConf `yaml:"log"`
DbConf DbConf `yaml:"db"`
KvDbConf KvDbConf `yaml:"kvdb"`
}
// FileExists检查某个文件路径是否存在
func FileExists(filename string) bool {
if filename == "" {
return false
}
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false
}
return true
}
func ParseConfig(file string) *AppConfiguration {
if !FileExists(file) {
// 正常启动
panic("app配置文件不存在 filename:" + file)
}
c := &AppConfiguration{}
b, err := ioutil.ReadFile(file)
if err != nil {
panic(fmt.Sprintf("读取app配置文件失败 filename:%s err:%s", file, err))
}
err = yaml.Unmarshal(b, c)
if err != nil || c.AppName == "" {
panic(fmt.Sprintf("解析app配置文件失败 filename:%s err:%s content:%s", file, err, string(b)))
}
AppConfig = c
return c
}
Go
1
https://gitee.com/home_robertzhai/go_common.git
git@gitee.com:home_robertzhai/go_common.git
home_robertzhai
go_common
go_common
v1.1.0

搜索帮助