1 Star 1 Fork 0

吴文凯/core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
config_model.go 5.78 KB
一键复制 编辑 原始数据 按行查看 历史
[email protected] 提交于 2021-09-16 17:13 . update config
package conf
import (
"fmt"
"reflect"
"strconv"
)
//todo 手动增加配置 conf.GetConf().Add()|.AddChild()
//todo 修改配置反写到配置文件
//todo 配置来源,from env\file\system
var conf Config
func GetConf() *Config {
return &conf
}
//总配置
type Config struct {
SysSetting sysSetting
Setting Settings
}
//系统设置
type sysSetting struct {
//环境 - 默认debug
//dev - 开发
//debug - 测试
//release - 正式
Env string
//http 监听端口地址 - 默认8080
HttpAddr string
//rpc 监听端口地址 - 默认30010
RpcAddr string
//本机ip
Host string
//系统名称
SystemName string
//从哪里读取配置 - 默认file,可选值file、acm(阿里配置中心)
ConfFrom string
}
//设置默认环境
func (s *sysSetting)SetDefaultEnv() {
if s.Env == "" {
s.Env = "debug"
}
}
//设置默认系统名称
func (s *sysSetting)SetDefaultSystemName() {
if s.SystemName == "" {
s.SystemName = "core"
}
}
//设置默认http监听地址
func (s *sysSetting)SetDefaultHttpAddr() {
if s.HttpAddr == "" {
s.HttpAddr = "8080"
}
}
//设置默认rpc监听地址
func (s *sysSetting)SetDefaultRpcAddr() {
if s.HttpAddr == "" {
s.HttpAddr = "30010"
}
}
//设置默认host
func (s *sysSetting)SetDefaultHost() {
if s.Host == "" {
s.Host = "127.0.0.1"
}
}
//设置默认值
func (s *sysSetting)SetDefault() {
s.SetDefaultSystemName()
s.SetDefaultEnv()
s.SetDefaultHttpAddr()
s.SetDefaultRpcAddr()
s.SetDefaultHost()
}
//其他设置
type Settings map[string]Setting
type Setting struct {
Key string
Value interface{}
change_chan []chan int
}
func Set(key string,v interface{}) {
conf.Setting.Set(key,v)
}
func (s *Settings)Set(key string,v interface{}) {
(*s)[key] = Setting{Key:key,Value:v}
}
func Get(key string) interface{} {
return conf.Setting.Get(key)
}
func (s *Settings)Get(key string) interface{} {
if v, ok := (*s)[key]; ok {
return v.Value
} else {
panic("key not find")
}
}
func GetInt(key string) int {
return conf.Setting.GetInt(key)
}
func (s *Settings)GetInt(key string) int {
tv := s.Get(key)
if v,ok := tv.(int);ok{
return v
}
switch reflect.TypeOf(tv).String() {
case "string":
if v,err := strconv.Atoi(tv.(string));err != nil{
panic(err)
}else {
return v
}
case "float32":
return int(tv.(float32))
case "float64":
return int(tv.(float64))
default:
panic("conf.GetInt():err type to int:"+reflect.TypeOf(tv).String())
}
}
func GetString(key string) string {
return conf.Setting.GetString(key)
}
func (s *Settings)GetString(key string) string {
return s.Get(key).(string)
}
func GetBool(key string) bool {
return conf.Setting.GetBool(key)
}
func (s *Settings)GetBool(key string) bool {
return s.Get(key).(bool)
}
func GetChild(key string) *Settings {
return conf.Setting.GetChild(key)
}
func (s *Settings)GetChild(key string) *Settings {
return s.Get(key).(*Settings)
}
func Getd(key string,d interface{}) interface{} {
return conf.Setting.Getd(key,d)
}
func (s *Settings)Getd(key string,d interface{})interface{} {
if s == nil {
return d
}
if v, ok := (*s)[key]; ok {
return v.Value
} else {
return d
}
}
func GetIntd(key string,d int) int {
return conf.Setting.GetIntd(key,d)
}
func (s *Settings)GetIntd(key string,d int) int {
tv := s.Get(key)
var v int
var ok bool
if v,ok = tv.(int);!ok{
switch reflect.TypeOf(tv).String() {
case "string":
if sv,err := strconv.Atoi(tv.(string));err != nil{
return d
}else {
v = sv
}
case "float32":
v = int(tv.(float32))
case "float64":
v = int(tv.(float64))
default:
return d
}
}
if v == 0 {
return d
}
return v
}
func GetStringd(key string,d string) string {
return conf.Setting.GetStringd(key,d)
}
func (s *Settings)GetStringd(key string,d string) string {
tv := s.Getd(key,d)
var v string
var ok bool
if v,ok = tv.(string);!ok {
switch reflect.TypeOf(tv).String() {
case "int":
v = strconv.Itoa(tv.(int))
}
}
if v == "" {
return d
}
return v
}
func GetBoold(key string,d bool) bool {
return conf.Setting.GetBoold(key,d)
}
func (s *Settings)GetBoold(key string,d bool) bool {
return s.Getd(key,d).(bool)
}
func GetChildd(key string) *Settings {
return conf.Setting.GetChildd(key)
}
func (s *Settings)GetChildd(key string) *Settings {
v := s.Getd(key,nil)
if v == nil {
return &Settings{}
}
return v.(*Settings)
}
func Getd_c(key string,d interface{}) (interface{},chan int) {
return conf.Setting.Getd_c(key,d)
}
func (s *Settings)Getd_c(key string,d interface{})(interface{},chan int) {
var value interface{}
change_chan := make(chan int)
if v, ok := (*s)[key]; ok {
value = v.Value
if change_chan == nil || len(v.change_chan) <= 0 {
v.change_chan = []chan int{}
}
v.change_chan = append(v.change_chan, change_chan)
(*s)[key] = v
} else {
value = d
}
return value,change_chan
}
func GetIntd_c(key string,d int) (int,chan int) {
return conf.Setting.GetIntd_c(key,d)
}
func (s *Settings)GetIntd_c(key string,d int) (int,chan int) {
v,ch := s.Getd_c(key,d)
return v.(int),ch
}
func GetStringd_c(key string,d string) (string,chan int) {
return conf.Setting.GetStringd_c(key,d)
}
func (s *Settings)GetStringd_c(key string,d string) (string,chan int) {
v,ch := s.Getd_c(key,d)
return v.(string),ch
}
func GetBoold_c(key string,d bool) (bool,chan int) {
return conf.Setting.GetBoold_c(key,d)
}
func (s *Settings)GetBoold_c(key string,d bool) (bool,chan int) {
v,ch := s.Getd_c(key,d)
return v.(bool),ch
}
func GetChildd_c(key string) (*Settings,chan int) {
return conf.Setting.GetChildd_c(key)
}
func (s *Settings)GetChildd_c(key string) (*Settings,chan int) {
v,ch := s.Getd_c(key,nil)
if v == nil {
return nil,ch
}
return v.(*Settings),ch
}
func (s *Setting)send_change() {
if s.change_chan != nil {
for _,t := range s.change_chan {
fmt.Println("配置修改:",s.Key)
t<-1
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/whilew/core-go.git
[email protected]:whilew/core-go.git
whilew
core-go
core-go
main

搜索帮助