forked from ark-network/ark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
146 lines (133 loc) · 4.63 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
142
143
144
145
146
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
common "github.com/ark-network/ark/common"
"github.com/spf13/viper"
)
type Config struct {
WalletAddr string
RoundInterval int64
Port uint32
EventDbType string
DbType string
DbDir string
SchedulerType string
TxBuilderType string
BlockchainScannerType string
NoTLS bool
Network common.Network
LogLevel int
MinRelayFee uint64
RoundLifetime int64
UnilateralExitDelay int64
AuthUser string
AuthPass string
}
var (
Datadir = "DATADIR"
WalletAddr = "WALLET_ADDR"
RoundInterval = "ROUND_INTERVAL"
Port = "PORT"
EventDbType = "EVENT_DB_TYPE"
DbType = "DB_TYPE"
SchedulerType = "SCHEDULER_TYPE"
TxBuilderType = "TX_BUILDER_TYPE"
BlockchainScannerType = "BC_SCANNER_TYPE"
Insecure = "INSECURE"
LogLevel = "LOG_LEVEL"
Network = "NETWORK"
MinRelayFee = "MIN_RELAY_FEE"
RoundLifetime = "ROUND_LIFETIME"
UnilateralExitDelay = "UNILATERAL_EXIT_DELAY"
AuthUser = "AUTH_USER"
AuthPass = "AUTH_PASS"
defaultDatadir = common.AppDataDir("arkd", false)
defaultRoundInterval = 5
defaultPort = 6000
defaultWalletAddr = "localhost:18000"
defaultDbType = "sqlite"
defaultEventDbType = "badger"
defaultSchedulerType = "gocron"
defaultTxBuilderType = "covenant"
defaultBlockchainScannerType = "ocean"
defaultInsecure = true
defaultNetwork = "liquid"
defaultLogLevel = 4
defaultMinRelayFee = 30 // 0.1 sat/vbyte on Liquid
defaultRoundLifetime = 604672
defaultUnilateralExitDelay = 1024
defaultAuthUser = "admin"
defaultAuthPass = "admin"
)
func LoadConfig() (*Config, error) {
viper.SetEnvPrefix("ARK")
viper.AutomaticEnv()
viper.SetDefault(Datadir, defaultDatadir)
viper.SetDefault(Port, defaultPort)
viper.SetDefault(DbType, defaultDbType)
viper.SetDefault(Insecure, defaultInsecure)
viper.SetDefault(LogLevel, defaultLogLevel)
viper.SetDefault(Network, defaultNetwork)
viper.SetDefault(WalletAddr, defaultWalletAddr)
viper.SetDefault(MinRelayFee, defaultMinRelayFee)
viper.SetDefault(RoundInterval, defaultRoundInterval)
viper.SetDefault(RoundLifetime, defaultRoundLifetime)
viper.SetDefault(SchedulerType, defaultSchedulerType)
viper.SetDefault(EventDbType, defaultEventDbType)
viper.SetDefault(TxBuilderType, defaultTxBuilderType)
viper.SetDefault(UnilateralExitDelay, defaultUnilateralExitDelay)
viper.SetDefault(BlockchainScannerType, defaultBlockchainScannerType)
viper.SetDefault(AuthUser, defaultAuthUser)
viper.SetDefault(AuthPass, defaultAuthPass)
net, err := getNetwork()
if err != nil {
return nil, err
}
if err := initDatadir(); err != nil {
return nil, fmt.Errorf("error while creating datadir: %s", err)
}
return &Config{
WalletAddr: viper.GetString(WalletAddr),
RoundInterval: viper.GetInt64(RoundInterval),
Port: viper.GetUint32(Port),
EventDbType: viper.GetString(EventDbType),
DbType: viper.GetString(DbType),
SchedulerType: viper.GetString(SchedulerType),
TxBuilderType: viper.GetString(TxBuilderType),
BlockchainScannerType: viper.GetString(BlockchainScannerType),
NoTLS: viper.GetBool(Insecure),
DbDir: filepath.Join(viper.GetString(Datadir), "db"),
LogLevel: viper.GetInt(LogLevel),
Network: net,
MinRelayFee: viper.GetUint64(MinRelayFee),
RoundLifetime: viper.GetInt64(RoundLifetime),
UnilateralExitDelay: viper.GetInt64(UnilateralExitDelay),
AuthUser: viper.GetString(AuthUser),
AuthPass: viper.GetString(AuthPass),
}, nil
}
func initDatadir() error {
datadir := viper.GetString(Datadir)
return makeDirectoryIfNotExists(datadir)
}
func makeDirectoryIfNotExists(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return os.MkdirAll(path, os.ModeDir|0755)
}
return nil
}
func getNetwork() (common.Network, error) {
switch strings.ToLower(viper.GetString(Network)) {
case "liquid":
return common.Liquid, nil
case "testnet":
return common.TestNet, nil
case "regtest":
return common.RegTest, nil
default:
return common.Network{}, fmt.Errorf("unknown network %s", viper.GetString(Network))
}
}