/
json.go
114 lines (100 loc) · 3.56 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 json
import (
"bytes"
"encoding/json"
"fmt"
"github.com/apolloconfig/agollo/v4/env/config"
"os"
"sync"
"github.com/apolloconfig/agollo/v4/component/log"
jsonConfig "github.com/apolloconfig/agollo/v4/env/config/json"
)
//Suffix 默认文件保存类型
const Suffix = ".json"
var (
//jsonFileConfig 处理文件的json格式存取
jsonFileConfig = &jsonConfig.ConfigFile{}
//configFileMap 存取namespace文件地址
configFileMap = make(map[string]string, 1)
//configFileMapLock configFileMap的读取锁
configFileMapLock sync.Mutex
//configFileDirMap 存取 configFileDir 文件所在地址,用于判断是否已经存在目录
configFileDirMap = make(map[string]bool, 1)
//configFileDirMapLock configFileDirMap的读取锁
configFileDirMapLock sync.Mutex
)
// FileHandler 默认备份文件读写
type FileHandler struct {
}
// WriteConfigFile write config to file
func (fileHandler *FileHandler) createDir(configPath string) error {
if configPath == "" {
return nil
}
configFileDirMapLock.Lock()
defer configFileDirMapLock.Unlock()
if !configFileDirMap[configPath] {
err := os.MkdirAll(configPath, os.ModePerm)
if err != nil && !os.IsExist(err) {
log.Errorf("Create backup dir:%s fail,error:&s", configPath, err)
return err
}
configFileDirMap[configPath] = true
}
return nil
}
// WriteConfigFile write config to file
func (fileHandler *FileHandler) WriteConfigFile(config *config.ApolloConfig, configPath string) error {
err := fileHandler.createDir(configPath)
if err != nil {
return err
}
return jsonFileConfig.Write(config, fileHandler.GetConfigFile(configPath, config.AppID, config.NamespaceName))
}
// GetConfigFile get real config file
func (fileHandler *FileHandler) GetConfigFile(configDir string, appID string, namespace string) string {
key := fmt.Sprintf("%s-%s", appID, namespace)
configFileMapLock.Lock()
defer configFileMapLock.Unlock()
fullPath := configFileMap[key]
if fullPath == "" {
filePath := fmt.Sprintf("%s%s", key, Suffix)
if configDir != "" {
configFileMap[namespace] = fmt.Sprintf("%s/%s", configDir, filePath)
} else {
configFileMap[namespace] = filePath
}
}
return configFileMap[namespace]
}
//LoadConfigFile load config from file
func (fileHandler *FileHandler) LoadConfigFile(configDir string, appID string, namespace string) (*config.ApolloConfig, error) {
configFilePath := fileHandler.GetConfigFile(configDir, appID, namespace)
log.Infof("load config file from: %s", configFilePath)
c, e := jsonFileConfig.Load(configFilePath, func(b []byte) (interface{}, error) {
config := &config.ApolloConfig{}
e := json.NewDecoder(bytes.NewBuffer(b)).Decode(config)
return config, e
})
if c == nil || e != nil {
log.Errorf("loadConfigFile fail,error:", e)
return nil, e
}
return c.(*config.ApolloConfig), e
}