Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee committed Jun 22, 2022
1 parent a02c34e commit 720800d
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 208 deletions.
4 changes: 2 additions & 2 deletions config/abc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ func (t CfgMap) GetString(name string) string {
return ""
}

type DecoderOption = viper.DecoderConfigOption
type Config interface {
LoadPath(path string)
LoadEnv(names ...string)
UnmarshalKey(key string, rawVal interface{}, opts ...viper.DecoderConfigOption) error
UnmarshalKey(key string, rawVal interface{}, opts ...DecoderOption) error
Decode(name string, cfgMap interface{}) error
Get(key string) interface{}
Set(string, interface{})
Expand Down
File renamed without changes.
7 changes: 4 additions & 3 deletions config/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package config

import (
"github.com/pubgo/dix"
"github.com/pubgo/xerror"
"github.com/pubgo/funk"
)

func init() {
defer funk.RecoverAndExit()
dix.Provider(func() Config { return newCfg() })
dix.Provider(func(c Config) *App {
var cfg App
xerror.Panic(c.UnmarshalKey("app", &cfg))
xerror.Panic(cfg.Check())
funk.Must(c.UnmarshalKey("app", &cfg))
funk.Must(cfg.Check())
return &cfg
})
}
16 changes: 7 additions & 9 deletions config/builder_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package config

import (
"github.com/pubgo/lava/internal/pkg/env"
"testing"

"github.com/smartystreets/assertions"
"github.com/smartystreets/assertions/should"
"github.com/pubgo/lava/internal/pkg/env"

"github.com/stretchr/testify/assert"
)

func TestName(t *testing.T) {
var assert = assertions.New(t)
var is = assert.New(t)

env.Set("env_prefix", "hello")
env.Set("hello_123", "app.name=hello")
env.Set("lava_hello", "hello")

var c = newCfg()
assert.So(c, should.NotBeNil)
assert.So(c.GetString("app.name"), should.Equal, "hello")
assert.So(c.GetString("app.home"), should.Equal, c.GetString("app.project"))
is.NotNil(c)
is.Equal(c.GetString("hello"), "hello")
}
64 changes: 39 additions & 25 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package config
import (
"fmt"
"io"
"io/fs"
"path/filepath"
"reflect"
"strings"
"sync"

"github.com/mitchellh/mapstructure"
"github.com/pubgo/funk"
"github.com/pubgo/x/iox"
"github.com/pubgo/x/pathutil"
"github.com/pubgo/xerror"
Expand All @@ -25,10 +27,11 @@ import (
)

var (
CfgType = "yaml"
CfgName = "config"
CfgDir = env.Get("cfg_dir", "app_cfg_dir")
CfgPath = filepath.Join("configs", "config", "config.yaml")
CfgType = "yaml"
CfgName = "config"
CfgDir = env.Get("app_cfg_home", "project_cfg_home", consts.EnvCfgHome)
CfgPath = filepath.Join("configs", "config", "config.yaml")
EnvPrefix = utils.FirstNotEmpty(env.Get("cfg_env_prefix", "app_env_prefix", "project_env_prefix", consts.EnvCfgPrefix), "lava")
)

// Init 处理所有的配置,环境变量和flag
Expand All @@ -37,8 +40,6 @@ var (
// flag可以指定配置文件位置
// 始化配置文件
func newCfg() *configImpl {
defer xerror.RecoverAndExit()

var t = &configImpl{v: viper.New()}
// 配置处理
v := t.v
Expand All @@ -47,40 +48,52 @@ func newCfg() *configImpl {
v.SetConfigType(CfgType)
v.SetConfigName(CfgName)
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
prefix := utils.FirstNotEmpty(env.Get("cfg_env_prefix", "env_prefix"), "lava")
v.SetEnvPrefix(prefix)
v.SetEnvPrefix(strings.ToUpper(EnvPrefix))
v.AutomaticEnv()

// 初始化框架, 加载环境变量, 加载本地配置
// 初始化完毕所有的配置以及外部配置以及相关的参数和变量
// 然后获取配置了
xerror.PanicF(t.initCfg(v), "config file load error")
t.LoadEnv()

CfgPath = v.ConfigFileUsed()
CfgDir = filepath.Dir(filepath.Dir(v.ConfigFileUsed()))
xerror.Panic(env.Set(consts.EnvCfgDir, CfgDir))
xerror.Panic(env.Set(consts.EnvCfgHome, CfgDir))
t.LoadPath(CfgPath)

// 加载自定义配置
//t.LoadPath(customCfgPath(app.Mode.String()))
//t.LoadPath(customCfgPath(app.Project))
t.loadCustomCfg()
return t
}

func customCfgPath(name string) string {
return filepath.Join(filepath.Dir(CfgPath), fmt.Sprintf("%s.%s.%s", CfgName, name, CfgType))
}

var _ Config = (*configImpl)(nil)

type configImpl struct {
rw sync.RWMutex
v *viper.Viper
}

func (t *configImpl) LoadEnv(names ...string) {
names = append(names, "cfg_env_prefix", "env_prefix")
loadEnvFromPrefix(env.Get(names...), t.v)
func (t *configImpl) loadCustomCfg() {
var path = filepath.Dir(t.v.ConfigFileUsed())
funk.MustMsg(filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() {
return nil
}

if !strings.HasSuffix(info.Name(), "."+CfgType) {
return nil
}

if info.Name() == CfgName+"."+CfgType {
return nil
}

t.LoadPath(path)
return nil
}), "walk path failed, path=%s", path)
}

func (t *configImpl) All() map[string]interface{} {
Expand Down Expand Up @@ -205,17 +218,17 @@ func (t *configImpl) initWithDir(v *viper.Viper) bool {
return false
}

xerror.Assert(pathutil.IsNotExist(CfgDir), "config dir not found, path:%s", CfgPath)
funk.Assert(pathutil.IsNotExist(CfgDir), "config dir not found, path:%s", CfgPath)
v.AddConfigPath(filepath.Join(CfgDir, CfgName))
xerror.PanicF(v.ReadInConfig(), "config load error, dir:%s", CfgDir)
funk.MustMsg(v.ReadInConfig(), "config load error, dir:%s", CfgDir)

return true
}

func (t *configImpl) initCfg(v *viper.Viper) (err error) {
defer xerror.RecoverErr(&err)

// 指定配置文件目录
// 指定配置目录
if t.initWithDir(v) {
return
}
Expand All @@ -226,7 +239,7 @@ func (t *configImpl) initCfg(v *viper.Viper) (err error) {
}

var pathList = strMap(getPathList(), func(str string) string { return filepath.Join(str, "configs", CfgName) })
xerror.Assert(len(pathList) == 0, "pathList is zero")
xerror.Assert(len(pathList) == 0, "config path not found")

for i := range pathList {
if t.addConfigPath(pathList[i]) {
Expand All @@ -243,6 +256,8 @@ func (t *configImpl) LoadPath(path string) {
return
}

fmt.Printf("load config %s\n", path)

tmp, err := fasttemplate.NewTemplate(xerror.PanicStr(iox.ReadText(path)), "{{", "}}")
xerror.Panic(err, "unexpected error when parsing template")

Expand All @@ -256,5 +271,4 @@ func (t *configImpl) LoadPath(path string) {

return w.Write([]byte(t.v.GetString(tag)))
}))))
t.LoadEnv()
}
32 changes: 2 additions & 30 deletions config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ package config

import (
"fmt"
"github.com/pubgo/lava/consts"
"github.com/pubgo/xerror"
"os"
"path/filepath"
"strings"

"github.com/iancoleman/strcase"
"github.com/pubgo/xerror"
"github.com/spf13/viper"

"github.com/pubgo/lava/consts"
"github.com/pubgo/lava/internal/pkg/env"
)

const pkgKey = "name"
Expand Down Expand Up @@ -51,28 +45,6 @@ func strMap(strList []string, fn func(str string) string) []string {
return strList
}

func loadEnvFromPrefix(envPrefix string, v *viper.Viper) {
if envPrefix == "" {
return
}

var r = strings.NewReplacer("-", "_", ".", "_", "/", "_")
envPrefix = strings.ToUpper(strings.ReplaceAll(r.Replace(strcase.ToSnake(envPrefix))+"_", "__", "_"))

for name, val := range env.List() {
if !strings.HasPrefix(name, envPrefix) || val == "" {
continue
}

envs := strings.SplitN(val, "=", 2)
if len(envs) != 2 || envs[0] == "" {
continue
}

v.Set(strings.TrimSpace(envs[0]), strings.TrimSpace(envs[1]))
}
}

func Decode[Cfg any](c Config, name string) map[string]Cfg {
var cfgMap = make(map[string]Cfg)
xerror.PanicF(c.Decode(name, &cfgMap), "config decode failed, name=%s", name)
Expand Down
21 changes: 13 additions & 8 deletions config/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@ import (
"github.com/pubgo/dix"
"github.com/pubgo/lava/internal/pkg/typex"
"github.com/pubgo/lava/vars"
"sort"
)

func init() {
vars.Register("config_data", func() interface{} {
var conf = new(struct {
C Config `inject:""`
})
dix.Inject(&conf)
var conf = dix.Inject(new(struct{ C Config }))
return conf.C.All()
})

vars.Register("config_keys", func() interface{} {
var conf = dix.Inject(new(struct{ C Config }))
var keys = conf.C.AllKeys()
sort.Strings(keys)
return keys
})

vars.Register("config", func() interface{} {
return typex.M{
"cfgType": CfgType,
"cfgName": CfgName,
"home": CfgDir,
"cfgPath": CfgPath,
"cfg_type": CfgType,
"cfg_name": CfgName,
"home": CfgDir,
"cfg_path": CfgPath,
}
})
}
26 changes: 26 additions & 0 deletions configs/config/1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
orm:
- name: default1
driver: "sqlite3"
driver_config:
dsn: "file::memory:?cache=shared"
- name: test1
driver: "sqlite3"
driver_config:
dsn: "file::memory:?cache=shared"
redis:
codis1:
addr: "127.0.0.1:6379"
password: "foobared"
db: 0
redisSale1:
addr: "127.0.0.1:6379"
password: "foobared"
db: 0
redisApiBusinessTwo1:
addr: "127.0.0.1:6379"
password: "foobared"
db: 0
redlock_0:
addr: "127.0.0.1:6379"
password: "foobared"
db: 123
Loading

0 comments on commit 720800d

Please sign in to comment.