上传配置中心系统接口代码

This commit is contained in:
liwei1dao 2022-06-01 13:41:31 +08:00
parent e9fc2df0a3
commit 0e1f31c0b9
5 changed files with 172 additions and 5 deletions

View File

@ -3,6 +3,8 @@ package web
import (
"go_dreamfactory/comm"
"go_dreamfactory/modules"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
"github.com/liwei1dao/lego/core"
)
@ -15,6 +17,7 @@ func NewModule() core.IModule {
type Web struct {
modules.ModuleBase
options *Options
table *cfg.TbItem
user_comp *User_Comp
}
@ -34,6 +37,16 @@ func (this *Web) Init(service core.IService, module core.IModule, options core.I
func (this *Web) Start() (err error) {
err = this.ModuleBase.Start()
var (
data interface{}
)
if err = configure.RegisterConfigure("tbitem.json", cfg.NewTbItem); err != nil {
return
}
if data, err = configure.GetConfigure("tbitem.json"); err != nil {
return
}
this.table = data.(*cfg.TbItem)
return
}

View File

@ -3,6 +3,7 @@ package services
import (
"fmt"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"github.com/liwei1dao/lego/base/rpcx"
@ -25,4 +26,9 @@ func (this *ServiceBase) InitSys() {
} else {
log.Infof("init sys.db success!")
}
if err := configure.OnInit(this.GetSettings().Sys["configure"]); err != nil {
panic(fmt.Sprintf("init sys.configure err: %s", err.Error()))
} else {
log.Infof("init sys.configure success!")
}
}

View File

@ -1,19 +1,152 @@
package configure
import "github.com/liwei1dao/lego/sys/redis"
import (
"fmt"
"io/ioutil"
"os"
"path"
"reflect"
"sync"
jsoniter "github.com/json-iterator/go"
)
var typeOfIn = reflect.TypeOf(([]map[string]interface{})(nil)).Elem()
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
type configurehandle struct {
configureType reflect.Type
fn reflect.Value
}
func newSys(options Options) (sys *Configure, err error) {
sys = &Configure{options: options}
sys = &Configure{
options: options,
configurehandles: map[string]*configurehandle{},
configure: map[string]interface{}{},
}
err = sys.init()
return
}
type Configure struct {
options Options
redis redis.ISys
hlock sync.RWMutex
configurehandles map[string]*configurehandle
clock sync.RWMutex
configure map[string]interface{}
}
func (this *Configure) init() (err error) {
return
}
//加载配置文件
func (this *Configure) RegisterConfigure(name string, fn interface{}) (err error) {
this.hlock.RLock()
_, ok := this.configurehandles[name]
this.hlock.RUnlock()
if ok {
err = fmt.Errorf("重复 注册配置【%s】", name)
return
}
fnvalue := reflect.ValueOf(fn)
if fnvalue.Type().NumIn() != 1 {
err = fmt.Errorf("LoadConfigure fn 类型错误! 只接受fn( _buf []map[string]interface{})(v,error) 函数参数")
return
}
inType := fnvalue.Type().In(0)
if inType.Elem() != typeOfIn {
err = fmt.Errorf("LoadConfigure fn 类型错误! 只接受fn( _buf []map[string]interface{})(v,error) 函数参数")
return
}
if fnvalue.Type().NumOut() != 2 {
err = fmt.Errorf("LoadConfigure fn 类型错误! 只接受fn( _buf []map[string]interface{})(v,error) 函数参数")
return
}
dataType := fnvalue.Type().Out(0)
if dataType.Kind() != reflect.Ptr {
err = fmt.Errorf("LoadConfigure fn 类型错误! 只接受fn( _buf []map[string]interface{})(v,error) 函数参数")
return
}
if returnType := fnvalue.Type().Out(1); returnType != typeOfError {
err = fmt.Errorf("LoadConfigure fn 类型错误! 只接受fn( _buf []map[string]interface{})(v,error) 函数参数")
return
}
handle := &configurehandle{
configureType: dataType,
fn: fnvalue,
}
if err = this.loaderConfigure(name, handle); err != nil {
return
}
this.hlock.Lock()
this.configurehandles[name] = handle
this.hlock.Unlock()
return
}
//更新配置信息
func (this *Configure) UpdateConfigure(names ...string) (err error) {
for _, v := range names {
this.hlock.RLock()
handle, ok := this.configurehandles[v]
this.hlock.RUnlock()
if !ok {
err = fmt.Errorf("no RegisterConfigure:%s", v)
return
}
if err = this.loaderConfigure(v, handle); err != nil {
err = fmt.Errorf("loaderConfigure:%s err:%v", v, err)
return
}
}
return
}
//读取配置文件
func (this *Configure) GetConfigure(name string) (v interface{}, err error) {
this.clock.RLock()
v, ok := this.configure[name]
this.clock.RUnlock()
if !ok {
err = fmt.Errorf("no LoadConfigure:%s", name)
}
return
}
//加载配置文件
func (this *Configure) loaderConfigure(name string, handle *configurehandle) (err error) {
var (
fliepath string
file *os.File
bytes []byte
data []map[string]interface{}
returnValues []reflect.Value
)
fliepath = path.Join(this.options.ConfigurePath, name)
if file, err = os.Open(fliepath); err != nil {
err = fmt.Errorf("no fond file:%s", fliepath)
return
}
defer file.Close()
if bytes, err = ioutil.ReadAll(file); err != nil {
err = fmt.Errorf("read file:%s err:%v", fliepath, err)
return
}
if err = jsoniter.Unmarshal(bytes, &data); err != nil {
err = fmt.Errorf("read file:%s json.Unmarshal err:%v", fliepath, err)
return
}
returnValues = handle.fn.Call([]reflect.Value{reflect.ValueOf(data)})
errInter := returnValues[1].Interface()
if errInter != nil {
err = fmt.Errorf("read file:%s load.fn err:%v", fliepath, errInter)
return
}
this.clock.Lock()
this.configure[name] = returnValues[0].Interface()
this.clock.Lock()
return
}

View File

@ -7,6 +7,9 @@ package configure
type (
ISys interface {
RegisterConfigure(name string, fn interface{}) (err error) //注册配置
UpdateConfigure(names ...string) (err error) //更新配置
GetConfigure(name string) (v interface{}, err error) //获取配置
}
)
@ -29,3 +32,15 @@ func NewSys(option ...Option) (sys ISys, err error) {
defsys, err = newSys(options)
return
}
func RegisterConfigure(name string, fn interface{}) (err error) {
return defsys.RegisterConfigure(name, fn)
}
func UpdateConfigure(names ...string) (err error) {
return defsys.UpdateConfigure(names...)
}
func GetConfigure(name string) (v interface{}, err error) {
return defsys.GetConfigure(name)
}

View File

@ -6,6 +6,7 @@ import (
type Option func(*Options)
type Options struct {
ConfigurePath string
}
func newOptions(config map[string]interface{}, opts ...Option) (Options, error) {
@ -16,7 +17,6 @@ func newOptions(config map[string]interface{}, opts ...Option) (Options, error)
for _, o := range opts {
o(&options)
}
return options, nil
}