OSDN Git Service

Add bytomd command line param about setting log level (#1115)
[bytom/bytom.git] / config / config.go
1 package config
2
3 import (
4         "os"
5         "os/user"
6         "path/filepath"
7         "runtime"
8         "time"
9 )
10
11 type Config struct {
12         // Top level options use an anonymous struct
13         BaseConfig `mapstructure:",squash"`
14         // Options for services
15         P2P    *P2PConfig     `mapstructure:"p2p"`
16         Wallet *WalletConfig  `mapstructure:"wallet"`
17         Auth   *RPCAuthConfig `mapstructure:"auth"`
18         Web    *WebConfig     `mapstructure:"web"`
19 }
20
21 // Default configurable parameters.
22 func DefaultConfig() *Config {
23         return &Config{
24                 BaseConfig: DefaultBaseConfig(),
25                 P2P:        DefaultP2PConfig(),
26                 Wallet:     DefaultWalletConfig(),
27                 Auth:       DefaultRPCAuthConfig(),
28                 Web:        DefaultWebConfig(),
29         }
30 }
31
32 // Set the RootDir for all Config structs
33 func (cfg *Config) SetRoot(root string) *Config {
34         cfg.BaseConfig.RootDir = root
35         cfg.P2P.RootDir = root
36         return cfg
37 }
38
39 //-----------------------------------------------------------------------------
40 // BaseConfig
41 type BaseConfig struct {
42         // The root directory for all data.
43         // This should be set in viper so it can unmarshal into this struct
44         RootDir string `mapstructure:"home"`
45
46         //The ID of the network to json
47         ChainID string `mapstructure:"chain_id"`
48
49         //log level to set
50         LogLevel string `mapstructure:"log_level"`
51
52         // A JSON file containing the private key to use as a validator in the consensus protocol
53         PrivateKey string `mapstructure:"private_key"`
54
55         // A custom human readable name for this node
56         Moniker string `mapstructure:"moniker"`
57
58         // TCP or UNIX socket address for the profiling server to listen on
59         ProfListenAddress string `mapstructure:"prof_laddr"`
60
61         // If this node is many blocks behind the tip of the chain, FastSync
62         // allows them to catchup quickly by downloading blocks in parallel
63         // and verifying their commits
64         FastSync bool `mapstructure:"fast_sync"`
65
66         Mining bool `mapstructure:"mining"`
67
68         FilterPeers bool `mapstructure:"filter_peers"` // false
69
70         // What indexer to use for transactions
71         TxIndex string `mapstructure:"tx_index"`
72
73         // Database backend: leveldb | memdb
74         DBBackend string `mapstructure:"db_backend"`
75
76         // Database directory
77         DBPath string `mapstructure:"db_dir"`
78
79         // Keystore directory
80         KeysPath string `mapstructure:"keys_dir"`
81
82         // remote HSM url
83         HsmUrl string `mapstructure:"hsm_url"`
84
85         ApiAddress string `mapstructure:"api_addr"`
86
87         VaultMode bool `mapstructure:"vault_mode"`
88
89         Time time.Time
90
91         // log file name
92         LogFile string `mapstructure:"log_file"`
93 }
94
95 // Default configurable base parameters.
96 func DefaultBaseConfig() BaseConfig {
97         return BaseConfig{
98                 Moniker:           "anonymous",
99                 ProfListenAddress: "",
100                 FastSync:          true,
101                 FilterPeers:       false,
102                 Mining:            false,
103                 TxIndex:           "kv",
104                 DBBackend:         "leveldb",
105                 DBPath:            "data",
106                 KeysPath:          "keystore",
107                 HsmUrl:            "",
108         }
109 }
110
111 func (b BaseConfig) DBDir() string {
112         return rootify(b.DBPath, b.RootDir)
113 }
114
115 func (b BaseConfig) KeysDir() string {
116         return rootify(b.KeysPath, b.RootDir)
117 }
118
119 // P2PConfig
120 type P2PConfig struct {
121         RootDir          string `mapstructure:"home"`
122         ListenAddress    string `mapstructure:"laddr"`
123         Seeds            string `mapstructure:"seeds"`
124         SkipUPNP         bool   `mapstructure:"skip_upnp"`
125         AddrBook         string `mapstructure:"addr_book_file"`
126         AddrBookStrict   bool   `mapstructure:"addr_book_strict"`
127         PexReactor       bool   `mapstructure:"pex"`
128         MaxNumPeers      int    `mapstructure:"max_num_peers"`
129         HandshakeTimeout int    `mapstructure:"handshake_timeout"`
130         DialTimeout      int    `mapstructure:"dial_timeout"`
131 }
132
133 // Default configurable p2p parameters.
134 func DefaultP2PConfig() *P2PConfig {
135         return &P2PConfig{
136                 ListenAddress:    "tcp://0.0.0.0:46656",
137                 AddrBook:         "addrbook.json",
138                 AddrBookStrict:   true,
139                 SkipUPNP:         false,
140                 MaxNumPeers:      50,
141                 HandshakeTimeout: 30,
142                 DialTimeout:      3,
143                 PexReactor:       true,
144         }
145 }
146
147 func (p *P2PConfig) AddrBookFile() string {
148         return rootify(p.AddrBook, p.RootDir)
149 }
150
151 //-----------------------------------------------------------------------------
152 type WalletConfig struct {
153         Disable bool `mapstructure:"disable"`
154         Rescan  bool `mapstructure:"rescan"`
155 }
156
157 type RPCAuthConfig struct {
158         Disable bool `mapstructure:"disable"`
159 }
160
161 type WebConfig struct {
162         Closed bool `mapstructure:"closed"`
163 }
164
165 // Default configurable rpc's auth parameters.
166 func DefaultRPCAuthConfig() *RPCAuthConfig {
167         return &RPCAuthConfig{
168                 Disable: false,
169         }
170 }
171
172 // Default configurable web parameters.
173 func DefaultWebConfig() *WebConfig {
174         return &WebConfig{
175                 Closed: false,
176         }
177 }
178
179 // Default configurable wallet parameters.
180 func DefaultWalletConfig() *WalletConfig {
181         return &WalletConfig{
182                 Disable: false,
183                 Rescan:  false,
184         }
185 }
186
187 //-----------------------------------------------------------------------------
188 // Utils
189
190 // helper function to make config creation independent of root dir
191 func rootify(path, root string) string {
192         if filepath.IsAbs(path) {
193                 return path
194         }
195         return filepath.Join(root, path)
196 }
197
198 // DefaultDataDir is the default data directory to use for the databases and other
199 // persistence requirements.
200 func DefaultDataDir() string {
201         // Try to place the data folder in the user's home dir
202         home := homeDir()
203         if home == "" {
204                 return "./.bytom"
205         }
206         switch runtime.GOOS {
207         case "darwin":
208                 return filepath.Join(home, "Library", "Bytom")
209         case "windows":
210                 return filepath.Join(home, "AppData", "Roaming", "Bytom")
211         default:
212                 return filepath.Join(home, ".bytom")
213         }
214 }
215
216 func homeDir() string {
217         if home := os.Getenv("HOME"); home != "" {
218                 return home
219         }
220         if usr, err := user.Current(); err == nil {
221                 return usr.HomeDir
222         }
223         return ""
224 }