feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
30
backend/infra/impl/dynconf/static/internal/common.go
Normal file
30
backend/infra/impl/dynconf/static/internal/common.go
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package internal
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func FileExist(name string) (existed bool, isDir bool) {
|
||||
info, err := os.Stat(name)
|
||||
if err != nil {
|
||||
return !os.IsNotExist(err), false
|
||||
}
|
||||
|
||||
return true, info.IsDir()
|
||||
}
|
||||
46
backend/infra/impl/dynconf/static/internal/raw_json.go
Normal file
46
backend/infra/impl/dynconf/static/internal/raw_json.go
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
// RawJsonData raw json object
|
||||
type RawJson struct {
|
||||
jsonBytes []byte
|
||||
}
|
||||
|
||||
func NewRawJson(path string) (*RawJson, error) {
|
||||
if path == "" {
|
||||
return nil, fmt.Errorf("[NewRawJson] path is nil")
|
||||
}
|
||||
|
||||
jsonBytes, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
y := &RawJson{jsonBytes: jsonBytes}
|
||||
|
||||
return y, err
|
||||
}
|
||||
|
||||
func (r *RawJson) GetBytes() []byte {
|
||||
return r.jsonBytes
|
||||
}
|
||||
47
backend/infra/impl/dynconf/static/internal/raw_yaml.go
Normal file
47
backend/infra/impl/dynconf/static/internal/raw_yaml.go
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
// RawYaml raw yaml object
|
||||
type RawYaml struct {
|
||||
yamlBytes []byte
|
||||
}
|
||||
|
||||
// NewRawYaml creates a object.
|
||||
func NewRawYaml(path string) (*RawYaml, error) {
|
||||
if path == "" {
|
||||
return nil, fmt.Errorf("[NewRawYaml] path is nil")
|
||||
}
|
||||
|
||||
yamlBytes, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
y := &RawYaml{yamlBytes: yamlBytes}
|
||||
|
||||
return y, err
|
||||
}
|
||||
|
||||
func (y *RawYaml) GetBytes() []byte {
|
||||
return y.yamlBytes
|
||||
}
|
||||
82
backend/infra/impl/dynconf/static/json.go
Normal file
82
backend/infra/impl/dynconf/static/json.go
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package static
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/infra/impl/dynconf/static/internal"
|
||||
)
|
||||
|
||||
type ConfJson struct {
|
||||
*internal.RawJson
|
||||
}
|
||||
|
||||
func NewConfJson(rootDir string, groups []string) (*ConfJson, error) {
|
||||
jsonFilePath := getJsonPath(rootDir, groups)
|
||||
if jsonFilePath == "" {
|
||||
return nil, ErrConfigNotExist
|
||||
}
|
||||
|
||||
json, err := internal.NewRawJson(jsonFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r := &ConfJson{json}
|
||||
return r, err
|
||||
}
|
||||
|
||||
func (c *ConfJson) MarshalFunc() MarshalFunc {
|
||||
return json.Marshal
|
||||
}
|
||||
|
||||
func (c *ConfJson) UnmarshalFunc() UnmarshalFunc {
|
||||
return json.Unmarshal
|
||||
}
|
||||
|
||||
func getJsonPath(rootDir string, groups []string) string {
|
||||
var findPaths []string
|
||||
|
||||
for r := len(groups); r > 0; r-- {
|
||||
findPaths = append(findPaths,
|
||||
filepath.Join(rootDir, fmt.Sprintf("config.%s", strings.Join(groups[:r], "."))))
|
||||
}
|
||||
|
||||
findPaths = append(findPaths, filepath.Join(rootDir, "config"))
|
||||
|
||||
for _, path := range findPaths {
|
||||
if p, exist := existJsonFile(path); exist {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func existJsonFile(fileName string) (string, bool) {
|
||||
p := fileName + ".json"
|
||||
existed, isDir := internal.FileExist(p)
|
||||
if existed && !isDir {
|
||||
return p, true
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
148
backend/infra/impl/dynconf/static/static_config.go
Normal file
148
backend/infra/impl/dynconf/static/static_config.go
Normal file
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package static
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultBase = "/deploy"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
absRepoRoot string // yaml文件root前缀的绝对路径
|
||||
useJson bool // 需要bind json文件,不指定默认bind yaml
|
||||
groups []string
|
||||
}
|
||||
|
||||
type OptFunc func(o *Options)
|
||||
|
||||
// WithAbsRepoRoot 传入自定义指定读取绝对路径/xx下的config.<xx>.(yaml,json),例如/opt/tiger/xxx
|
||||
func WithAbsRepoRoot(absRepoRoot string) OptFunc {
|
||||
return func(o *Options) {
|
||||
if len(absRepoRoot) > 0 {
|
||||
o.absRepoRoot = absRepoRoot
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithUseJSONType 需要查找xx.json结尾的文
|
||||
func WithUseJSONType(useJson bool) OptFunc {
|
||||
return func(o *Options) {
|
||||
o.useJson = useJson
|
||||
}
|
||||
}
|
||||
|
||||
func WithGroups(groups []string) OptFunc {
|
||||
return func(o *Options) {
|
||||
o.groups = groups
|
||||
}
|
||||
}
|
||||
|
||||
func loadOpts(opts ...OptFunc) *Options {
|
||||
o := &Options{}
|
||||
for _, opt := range opts {
|
||||
opt(o)
|
||||
}
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
var configers sync.Map // key: abs path, value: configer
|
||||
|
||||
// New 可传入local_config_dir指定读取自定义的绝对路径文件 `<local_config_dir>/config.<env>.<region>.<cluster>.yaml`
|
||||
func getOrCreateConf(opts ...OptFunc) (configer, error) {
|
||||
options := loadOpts(opts...)
|
||||
if options.absRepoRoot == "" {
|
||||
options.absRepoRoot = defaultBase
|
||||
}
|
||||
|
||||
keyFn := func(dir string, withJSON bool) string {
|
||||
fileFmt := "yaml"
|
||||
if withJSON {
|
||||
fileFmt = "json"
|
||||
}
|
||||
return dir + "_" + fileFmt
|
||||
}
|
||||
dir := options.absRepoRoot
|
||||
withJSON := options.useJson
|
||||
key := keyFn(dir, withJSON)
|
||||
|
||||
val, exist := configers.Load(key)
|
||||
if exist {
|
||||
if conf, ok := val.(configer); ok {
|
||||
return conf, nil
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
cfg configer
|
||||
err error
|
||||
)
|
||||
|
||||
if withJSON {
|
||||
cfg, err = NewConfJson(dir, options.groups)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
cfg, err = NewConfYaml(dir, options.groups) // 默认使用yaml
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
configers.Store(key, cfg)
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// JSONBind 不传dir值,按默认路径,优先级读取/opt/tiger/flowdevops/confcenter/psm/p.s.m/config.<env>.<region>.<cluster>.json
|
||||
// 可使用WithAbsRepoRoot 传入自定义指定读取
|
||||
func JSONBind(structPtr interface{}, opts ...OptFunc) error {
|
||||
opts = append(opts, WithUseJSONType(true))
|
||||
conf, err := getOrCreateConf(opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("find config failed: %w", err)
|
||||
}
|
||||
return bindAndValidate(structPtr, conf)
|
||||
}
|
||||
|
||||
// YAMLBind 不传dir值,按默认路径,按优先级读取/opt/tiger/flowdevops/confcenter/psm/p.s.m/config.<env>.<region>.<cluster>.yaml
|
||||
// 可使用WithAbsRepoRoot 传入自定义指定读取
|
||||
func YAMLBind(structPtr interface{}, opts ...OptFunc) error {
|
||||
conf, err := getOrCreateConf(opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("find config failed: %w", err)
|
||||
}
|
||||
return bindAndValidate(structPtr, conf)
|
||||
}
|
||||
|
||||
type MarshalFunc func(data interface{}) ([]byte, error)
|
||||
type UnmarshalFunc func(data []byte, v interface{}) error
|
||||
|
||||
type configer interface {
|
||||
GetBytes() []byte
|
||||
MarshalFunc() MarshalFunc
|
||||
UnmarshalFunc() UnmarshalFunc
|
||||
}
|
||||
|
||||
func bindAndValidate(structPtr interface{}, cnf configer) error {
|
||||
return cnf.UnmarshalFunc()(cnf.GetBytes(), structPtr)
|
||||
}
|
||||
92
backend/infra/impl/dynconf/static/yaml.go
Normal file
92
backend/infra/impl/dynconf/static/yaml.go
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package static
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/infra/impl/dynconf/static/internal"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrConfigNotExist = errors.New("config not exist")
|
||||
)
|
||||
|
||||
type ConfYaml struct {
|
||||
*internal.RawYaml
|
||||
filepath string
|
||||
}
|
||||
|
||||
func NewConfYaml(rootDir string, groups []string) (*ConfYaml, error) {
|
||||
yamlFilePath := getYamlPath(rootDir, groups)
|
||||
if yamlFilePath == "" {
|
||||
return nil, ErrConfigNotExist
|
||||
}
|
||||
|
||||
yaml, err := internal.NewRawYaml(yamlFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r := &ConfYaml{RawYaml: yaml, filepath: yamlFilePath}
|
||||
|
||||
return r, err
|
||||
}
|
||||
|
||||
func (c *ConfYaml) MarshalFunc() MarshalFunc {
|
||||
return yaml.Marshal
|
||||
}
|
||||
|
||||
func (c *ConfYaml) UnmarshalFunc() UnmarshalFunc {
|
||||
return yaml.Unmarshal
|
||||
}
|
||||
|
||||
func getYamlPath(rootDir string, groups []string) string {
|
||||
var findPaths []string
|
||||
|
||||
for r := len(groups); r > 0; r-- {
|
||||
findPaths = append(findPaths,
|
||||
filepath.Join(rootDir, fmt.Sprintf("config.%s", strings.Join(groups[:r], "."))))
|
||||
}
|
||||
|
||||
findPaths = append(findPaths, filepath.Join(rootDir, "config"))
|
||||
|
||||
for _, path := range findPaths {
|
||||
if p, exist := existYamlFile(path); exist {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func existYamlFile(yamlFileName string) (string, bool) {
|
||||
for _, ext := range []string{".yml", ".yaml"} {
|
||||
p := yamlFileName + ext
|
||||
existed, isDir := internal.FileExist(p)
|
||||
if existed && !isDir {
|
||||
return p, true
|
||||
}
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
Reference in New Issue
Block a user