-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.go
More file actions
40 lines (34 loc) · 932 Bytes
/
common.go
File metadata and controls
40 lines (34 loc) · 932 Bytes
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
package config
import (
"io"
"net/http"
"os"
"github.com/goal-web/supports/logs"
)
type EnvProvider func() []byte
// File 以本地文件作为配置数据源,读取失败时返回空字节并记录调试日志。
func File(path string) EnvProvider {
return func() []byte {
tmpBytes, err := os.ReadFile(path)
if err != nil {
log.Debug("File: " + err.Error())
}
return tmpBytes
}
}
// Url 以远程 URL 作为配置数据源,网络或读取失败时返回 nil 并记录调试日志。
func Url(url string) EnvProvider {
return func() []byte {
res, err := http.Get(url)
if err != nil {
log.Debug("File: " + err.Error())
return nil
}
tmpBytes, err := io.ReadAll(res.Body)
if err != nil {
log.Debug("File: " + err.Error())
}
return tmpBytes
}
}
var log = logs.Default()