OSDN Git Service

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