go_dreamfactory/modules/options.go
2022-07-26 15:14:37 +08:00

75 lines
1.3 KiB
Go

package modules
import (
"fmt"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/utils/mapstructure"
"time"
)
type (
IOptions interface {
core.IModuleOptions
GetDebug() bool
GetLog() log.ILog
}
Options struct {
Debug bool //日志是否开启
Log log.ILog
}
)
func (this *Options) GetDebug() bool {
return this.Debug
}
func (this *Options) GetLog() log.ILog {
return this.Log
}
func (this *Options) LoadConfig(settings map[string]interface{}) (err error) {
if settings != nil {
err = mapstructure.Decode(settings, this)
}
if this.Debug && this.Log == nil {
this.Log = log.Clone()
if this.Log == nil {
err = fmt.Errorf("Log is nil")
return
}
}
return
}
type DBOption func(*DBOptions)
type DBOptions struct {
IsMgoLog bool //是否写mgolog
Expire time.Duration //过期时间
}
//设置是否写mgor日志
func SetDBMgoLog(v bool) DBOption {
return func(o *DBOptions) {
o.IsMgoLog = v
}
}
//设置过期时间
func SetDBExpire(v time.Duration) DBOption {
return func(o *DBOptions) {
o.Expire = v
}
}
//更具 Option 序列化 系统参数对象
func newDBOption(opts ...DBOption) DBOptions {
options := DBOptions{
IsMgoLog: true,
}
for _, o := range opts {
o(&options)
}
return options
}