Skip to content

Commit 6848d46

Browse files
committed
fix: update config load
1 parent cabee74 commit 6848d46

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-19
lines changed

config/builder_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import (
1111
func TestName(t *testing.T) {
1212
var is = assert.New(t)
1313

14-
env.Set("lava_hello", "hello")
14+
_ = env.Set("lava_hello", "hello")
1515

1616
var c = newCfg()
1717
is.NotNil(c)
1818
is.Equal(c.GetString("hello"), "hello")
19+
t.Log(c.All())
1920
}

config/config.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package config
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
67
"io/fs"
78
"path/filepath"
89
"reflect"
910
"strings"
1011
"sync"
12+
"text/template"
1113

1214
"github.com/mitchellh/mapstructure"
1315
"github.com/pubgo/funk"
@@ -16,7 +18,6 @@ import (
1618
"github.com/pubgo/xerror"
1719
"github.com/spf13/cast"
1820
"github.com/spf13/viper"
19-
"github.com/valyala/fasttemplate"
2021

2122
"github.com/pubgo/lava/consts"
2223
"github.com/pubgo/lava/internal/pkg/env"
@@ -47,7 +48,7 @@ func newCfg() *configImpl {
4748
// 配置文件名字和类型
4849
v.SetConfigType(CfgType)
4950
v.SetConfigName(CfgName)
50-
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
51+
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_", "/", "_"))
5152
v.SetEnvPrefix(strings.ToUpper(EnvPrefix))
5253
v.AutomaticEnv()
5354

@@ -258,17 +259,22 @@ func (t *configImpl) LoadPath(path string) {
258259

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

261-
tmp, err := fasttemplate.NewTemplate(xerror.PanicStr(iox.ReadText(path)), "{{", "}}")
262-
xerror.Panic(err, "unexpected error when parsing template")
263-
264-
// 重新加载配置
265-
xerror.Panic(t.v.MergeConfig(strings.NewReader(tmp.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
266-
tag = strings.TrimSpace(tag)
267-
// 处理配置中的环境变量
268-
if strings.HasPrefix(tag, "$") {
269-
return w.Write([]byte(env.Get(tag)))
270-
}
271-
272-
return w.Write([]byte(t.v.GetString(tag)))
273-
}))))
262+
tmpl := funk.Must1(template.New("").Funcs(template.FuncMap{
263+
"upper": strings.ToUpper,
264+
"env": env.Get,
265+
"trim": strings.TrimSpace,
266+
"v": t.v.GetString,
267+
"default": func(a string, b string) string {
268+
if strings.TrimSpace(b) == "" {
269+
return a
270+
}
271+
return b
272+
},
273+
}).Parse(funk.Must1(iox.ReadText(path))))
274+
275+
var buf bytes.Buffer
276+
funk.Must(tmpl.Execute(&buf, map[string]string{}))
277+
278+
// 合并配置
279+
funk.Must(t.v.MergeConfig(&buf))
274280
}

config/util.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package config
22

33
import (
44
"fmt"
5-
"github.com/pubgo/lava/consts"
6-
"github.com/pubgo/xerror"
75
"os"
86
"path/filepath"
7+
8+
"github.com/pubgo/lava/consts"
9+
"github.com/pubgo/xerror"
910
)
1011

1112
const pkgKey = "name"

config/vars.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package config
22

33
import (
4+
"sort"
5+
46
"github.com/pubgo/dix"
7+
58
"github.com/pubgo/lava/internal/pkg/typex"
69
"github.com/pubgo/lava/vars"
7-
"sort"
810
)
911

1012
func init() {

configs/config/1.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ orm:
77
driver: "sqlite3"
88
driver_config:
99
dsn: "file::memory:?cache=shared"
10+
data: '{{env "lava_hello" "lava_abc" | default "world"}}'
11+
home: '{{env "home"}}'
12+
other: '{{v "redis.codis.addr"}}'
1013
redis:
1114
codis1:
1215
addr: "127.0.0.1:6379"

0 commit comments

Comments
 (0)