70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
package db
|
|
|
|
import (
|
|
"go_dreamfactory/lego/utils/mapstructure"
|
|
)
|
|
|
|
//DB层配置
|
|
type DBConfig struct {
|
|
Enabled bool //是否生效
|
|
RedisIsCluster bool //是否是集群
|
|
RedisAddr []string //redis 的集群地址
|
|
RedisPassword string //redis的密码
|
|
RedisDB int //数据库位置
|
|
MongodbUrl string
|
|
MongodbDatabase string
|
|
}
|
|
|
|
type Option func(*Options)
|
|
type Options struct {
|
|
Loacl DBConfig //本服配置
|
|
Cross DBConfig //跨服配置
|
|
ServerList map[string]DBConfig //服务列表配置
|
|
}
|
|
|
|
//设置本服配置
|
|
func SetLoacl(v DBConfig) Option {
|
|
return func(o *Options) {
|
|
o.Loacl = v
|
|
}
|
|
}
|
|
|
|
//设置本服配置
|
|
func SetCross(v DBConfig) Option {
|
|
return func(o *Options) {
|
|
o.Cross = v
|
|
}
|
|
}
|
|
|
|
//设置跨服区服列表
|
|
func SetServerList(v map[string]DBConfig) Option {
|
|
return func(o *Options) {
|
|
o.ServerList = v
|
|
}
|
|
}
|
|
|
|
func newOptions(config map[string]interface{}, opts ...Option) (options *Options, err error) {
|
|
options = &Options{
|
|
Loacl: DBConfig{},
|
|
Cross: DBConfig{},
|
|
ServerList: make(map[string]DBConfig),
|
|
}
|
|
if config != nil {
|
|
if err = mapstructure.Decode(config, options); err != nil {
|
|
return
|
|
}
|
|
}
|
|
for _, o := range opts {
|
|
o(options)
|
|
}
|
|
return
|
|
}
|
|
|
|
func newOptionsByOption(opts ...Option) (options *Options, err error) {
|
|
options = &Options{}
|
|
for _, o := range opts {
|
|
o(options)
|
|
}
|
|
return
|
|
}
|