-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
48 lines (44 loc) · 1.27 KB
/
config.go
File metadata and controls
48 lines (44 loc) · 1.27 KB
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
package common
import (
"fmt"
"net/http"
"os"
"path"
"github.com/BurntSushi/toml"
)
func LoadConfig(config any, configdir, app string) {
// 读区business.form.toml,并发送http://configserver/config/render?app=$app
// 读取文件
// 使用本地配置文件覆盖远程文件, 开发人员自己保证
file, err := os.Open(path.Join(configdir, "business.form.toml"))
if err == nil {
resp, err := http.Post("http://configserver/config/render?app="+app, "text/text", file)
if err == nil {
defer resp.Body.Close()
_, err = toml.NewDecoder(resp.Body).Decode(config)
if err != nil {
panic(err)
}
} else {
fmt.Printf("failed to post business.form.toml to configserver reason: %s\n", err.Error())
}
} else {
fmt.Printf("failed to read business.form.toml reason: %s\n", err.Error())
}
LoadConfigFile(config, path.Join(configdir, "business.public.toml"))
LoadConfigFile(config, path.Join(configdir, "business.private.toml"))
}
func LoadConfigFile(config any, file string) bool {
fmt.Printf("load config file %s\n", file)
buf, err := os.ReadFile(file)
if err == nil {
_, err = toml.Decode(string(buf), config)
if err != nil {
panic(err)
}
return true
} else {
fmt.Printf("failed to read %s reason: %s\n", file, err.Error())
}
return false
}