/
config.go
141 lines (121 loc) · 3.27 KB
/
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package config
import (
"bytes"
"context"
"io/ioutil"
"os"
"path/filepath"
"github.com/azer/logger"
"github.com/bitspill/floutil"
"github.com/gobuffalo/packr/v2"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
appDir string
defaultAppDir = floutil.AppDataDir("oipd", false)
configBox = packr.New("defaults", "./defaults")
subs []func(context.Context)
testnetState *bool
)
func init() {
logger.SetOutput(os.Stdout)
loadDefaults()
pflag.String("appdir", defaultAppDir, "Location of oip data directory and config file")
pflag.String("cpuprofile", "", "Designates the file to use for the cpu profiler")
pflag.String("memprofile", "", "Designates the file to use for the memory profiler")
pflag.Parse()
err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
panic(err)
}
appDir = viper.GetString("appdir")
b, err := configBox.Find("config.example.yml")
if err != nil {
panic(err)
}
err = viper.ReadConfig(bytes.NewReader(b))
if err != nil {
panic(err)
}
viper.SetConfigName("config")
viper.AddConfigPath(appDir)
err = viper.ReadInConfig()
if err != nil {
log.Error("error loading config file, utilizing defaults", logger.Attrs{"err": err})
confFile := filepath.Join(appDir, "config.yml")
_, err = os.Stat(confFile)
if os.IsNotExist(err) {
log.Info("config.yml not found, writing default config file", logger.Attrs{"confFile": confFile})
err = os.MkdirAll(appDir, 0755)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(confFile, b, 0600)
if err != nil {
panic(err)
}
} else if err != nil {
panic(err)
}
}
}
func loadDefaults() {
// command line flag to change config directory
viper.SetDefault("appdir", defaultAppDir)
// Elastic defaults
viper.SetDefault("elastic.host", "http://127.0.0.1:9200")
viper.SetDefault("elastic.useCert", false)
viper.SetDefault("elastic.certFile", "certs/oipd.pem")
viper.SetDefault("elastic.certKey", "certs/oipd.key")
viper.SetDefault("elastic.certRoot", "certs/root-ca.pem")
// Flod defaults
defaultFlodDir := floutil.AppDataDir("flod", false)
defaultFlodCert := filepath.Join(defaultFlodDir, "rpc.cert")
viper.SetDefault("flod.certFile", defaultFlodCert)
viper.SetDefault("flod.tls", true)
viper.SetDefault("flod.host", "127.0.0.1:8334")
viper.SetDefault("flod.user", "user")
viper.SetDefault("flod.pass", "pass")
// Testnet defaults
viper.SetDefault("oip.network", "mainnet")
// HttpApi defaults
viper.SetDefault("oip.api.listen", "127.0.0.1:1606")
viper.SetDefault("oip.api.enabled", false)
// oip5 defaults
viper.SetDefault("oip.oip5.publisherCacheDepth", 1000)
viper.SetDefault("oip.oip5.recordCacheDepth", 10000)
}
func IsTestnet() bool {
if testnetState == nil {
b := viper.GetString("oip.network") != "mainnet"
testnetState = &b
}
return *testnetState
}
func SetTestnet(testnet bool) {
n := "mainnet"
if testnet {
n = "testnet"
}
viper.Set("oip.network", n)
if testnetState == nil {
testnetState = &testnet
}
*testnetState = testnet
}
func GetFilePath(key string) string {
v := viper.GetString(key)
if filepath.IsAbs(v) {
return v
}
return filepath.Join(appDir, v)
}
func OnPostConfig(fn func(context.Context)) {
subs = append(subs, fn)
}
func PostConfig(ctx context.Context) {
for _, fn := range subs {
fn(ctx)
}
}