/
json.go
68 lines (55 loc) · 1.5 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
package propertyresolver
import (
"encoding/json"
"io/ioutil"
"os"
"strings"
"github.com/TIBCOSoftware/flogo-lib/app"
"github.com/TIBCOSoftware/flogo-lib/logger"
)
var preload = make(map[string]interface{})
var log = logger.GetLogger("app-props-json-resolver")
// Comma separated list of json files overriding default application property values
// e.g. FLOGO_APP_PROPS_JSON=app1.json,common.json
const EnvAppPropertyFileConfigKey = "FLOGO_APP_PROPS_JSON"
func init() {
filePaths := getExternalFiles()
if filePaths != "" {
// Register value resolver
app.RegisterPropertyValueResolver("json", &JSONFileValueResolver{})
// preload props from files
files := strings.Split(filePaths, ",")
if len(files) > 0 {
for _, filePath := range files {
props := make(map[string]interface{})
file, e := ioutil.ReadFile(filePath)
if e != nil {
log.Errorf("Can not read - %s due to error - %v", filePath, e)
panic("")
}
e = json.Unmarshal(file, &props)
if e != nil {
log.Errorf("Can not read - %s due to error - %v", filePath, e)
panic("")
}
for k, v := range props {
preload[k] = v
}
}
}
}
}
func getExternalFiles() string {
key := os.Getenv(EnvAppPropertyFileConfigKey)
if len(key) > 0 {
return key
}
return ""
}
// Resolve property value from external files
type JSONFileValueResolver struct {
}
func (resolver *JSONFileValueResolver) LookupValue(toResolve string) (interface{}, bool) {
val, found := preload[toResolve]
return val, found
}