Ai
2 Star 0 Fork 0

GGGGDDD/Gin-Cli-SQL

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
settings.go 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
GGGGDDD 提交于 2022-09-10 12:09 +08:00 . 初始化Gin脚手架
package settings
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
var Conf = new(AppConfig) // 从该对象访问数据
type AppConfig struct {
Mode string `mapstructure:"mode"`
Port int `mapstructure:"port"`
*LogConfig `mapstructure:"log"`
*MySQLConfig `mapstructure:"mysql"`
*RedisConfig `mapstructure:"redis"`
}
type MySQLConfig struct {
Host string `mapstructure:"host"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
DB string `mapstructure:"db"`
Port int `mapstructure:"port"`
MaxOpenConns int `mapstructure:"max_open_conns"`
MaxIdleConns int `mapstructure:"max_idle_conns"`
}
type RedisConfig struct {
Host string `mapstructure:"host"`
Password string `mapstructure:"password"`
Port int `mapstructure:"port"`
DB int `mapstructure:"db"`
PoolSize int `mapstructure:"pool_size"`
MinIdleConns int `mapstructure:"min_idle_conns"`
}
type LogConfig struct {
Level string `mapstructure:"level"`
Filename string `mapstructure:"filename"`
MaxSize int `mapstructure:"max_size"`
MaxAge int `mapstructure:"max_age"`
MaxBackups int `mapstructure:"max_backups"`
}
// Init mapstructure的作用:
// 正常处理过程是将json/yaml等二进制数据转化成map之后,再转化成struct对象。
// 通过mapstructure可以直接将json/yaml等二进制数据转化成struct对象。
func Init() error {
viper.SetConfigFile("./conf/config.yaml")
viper.WatchConfig() // 监控配置文件变化
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("配置文件变化:", e.Name)
viper.Unmarshal(&Conf)
})
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("ReadInConfig failed, err: %v", err))
}
if err := viper.Unmarshal(&Conf); err != nil {
panic(fmt.Errorf("unmarshal failed, err: %v", err))
}
return err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/miaokela/gin-cli-sql.git
git@gitee.com:miaokela/gin-cli-sql.git
miaokela
gin-cli-sql
Gin-Cli-SQL
master

搜索帮助