-
Notifications
You must be signed in to change notification settings - Fork 1
/
singleton.go
58 lines (48 loc) · 1.01 KB
/
singleton.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
package cfg
import (
"context"
"log"
"os"
)
var instance = must()
const (
cfgDebug = "CFG_DEBUG"
defaultPath = "/config.json"
pathEnv = "CFG_LOCATION"
hotReloadEnv = "CFG_SKIP_HOT_RELOAD"
)
func Get(key string) string {
return instance.Get(key)
}
func SetErrorf(fn func(ctx context.Context, err error, format string, args ...interface{})) {
instance.opts.Errorf = fn
}
func SetInfof(fn func(ctx context.Context, format string, args ...interface{})) {
instance.opts.Infof = fn
}
func SetWarnf(fn func(ctx context.Context, format string, args ...interface{})) {
instance.opts.Warnf = fn
}
func Rebuild() {
instance = must()
}
func must() *cfg {
var path = defaultPath
if p := os.Getenv(pathEnv); p != "" {
path = p
}
var hotReload = true
if h := os.Getenv(hotReloadEnv); h == "true" {
hotReload = false
}
c, err := New(Opts{
Path: path,
HotReload: hotReload,
})
if err != nil {
if os.Getenv(cfgDebug) == "true" {
log.Printf("cfg::New: %s", err.Error())
}
}
return &c
}