/
init_config.go
61 lines (50 loc) · 1.41 KB
/
init_config.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
// Package serve
//
// @author: xwc1125
package serve
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/tidwall/gjson"
)
func (p *ProxyServe) initSchema(dir string) {
var (
apisixSchemaPath = dir + "/conf/schema.json"
apisixSchemaContent []byte
customizeSchemaContent []byte
err error
)
if apisixSchemaContent, err = ioutil.ReadFile(apisixSchemaPath); err != nil {
panic(fmt.Errorf("fail to read configuration: %s, error: %s", apisixSchemaPath, err.Error()))
}
content, err := mergeSchema(apisixSchemaContent, customizeSchemaContent)
if err != nil {
panic(err)
}
p.schema = gjson.ParseBytes(content)
}
func mergeSchema(apisixSchema, customizeSchema []byte) ([]byte, error) {
var (
apisixSchemaMap map[string]map[string]interface{}
customizeSchemaMap map[string]map[string]interface{}
)
if len(customizeSchema) == 0 {
return apisixSchema, nil
}
if err := json.Unmarshal(apisixSchema, &apisixSchemaMap); err != nil {
return nil, err
}
if err := json.Unmarshal(customizeSchema, &customizeSchemaMap); err != nil {
return nil, err
}
for key := range apisixSchemaMap["main"] {
if _, ok := customizeSchemaMap["main"][key]; ok {
return nil, fmt.Errorf("duplicates key: main.%s between schema.json and customize_schema.json", key)
}
}
for k, v := range customizeSchemaMap["main"] {
apisixSchemaMap["main"][k] = v
}
return json.Marshal(apisixSchemaMap)
}