|
1 | 1 | package utils |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
| 4 | + "os" |
| 5 | + "strconv" |
5 | 6 | "time" |
6 | | -) |
7 | | - |
8 | | -const ( |
9 | | - hostNodeBTC = "1212.154.54" |
10 | | - loginNodeBTC = "" |
11 | | -) |
12 | 7 |
|
13 | | -var ( |
14 | | - urlBTC = fmt.Sprintf("https://%s:%s@127.0.0.1:8332", loginNodeBTC, hostNodeBTC) |
| 8 | + "github.com/joho/godotenv" |
15 | 9 | ) |
16 | 10 |
|
17 | 11 | type RPCConfig struct { |
18 | | - hostNodeBTC string |
19 | | - loginNodeBTC string |
20 | | - urlBTC string |
21 | | - ServerPingTime time.Duration `long:"server-ping-time" description:"How long the server waits on a gRPC stream with no activity before pinging the client."` |
| 12 | + Host string |
| 13 | + Port string |
| 14 | + RPCUser string |
| 15 | + RPCPassword string |
| 16 | + IsHTTPS bool |
22 | 17 | } |
23 | 18 |
|
24 | 19 | type Config struct { |
25 | | - nodeType string |
26 | | - walletVersion string |
27 | | - rpcConfig RPCConfig |
| 20 | + NodeType string |
| 21 | + WalletVersion string |
| 22 | + RPCConfig RPCConfig |
| 23 | + ServerPingTime time.Duration |
28 | 24 | } |
29 | 25 |
|
30 | 26 | func DefaultConfig() *Config { |
31 | 27 | return &Config{ |
32 | | - nodeType: "mainnet", |
33 | | - walletVersion: "2.0", |
34 | | - rpcConfig : &RPCConfig{ |
35 | | - hostNodeBTC: hostNodeBTC, |
36 | | - loginNodeBTC: loginNodeBTC, |
37 | | - urlBTC: urlBTC, |
38 | | - ServerPingTime: 10 * time.Second, |
| 28 | + NodeType: "mainnet", |
| 29 | + WalletVersion: "2.0", |
| 30 | + ServerPingTime: 10 * time.Second, |
| 31 | + RPCConfig: RPCConfig{ |
| 32 | + Host: "127.0.0.1", |
| 33 | + Port: "8332", |
| 34 | + RPCUser: "user", |
| 35 | + RPCPassword: "password", |
| 36 | + IsHTTPS: false, |
| 37 | + }, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func LoadConfig() (*Config, error) { |
| 42 | + _ = godotenv.Load() |
| 43 | + |
| 44 | + config := DefaultConfig() |
| 45 | + |
| 46 | + if value, exists := os.LookupEnv("NODE_TYPE"); exists && value != "" { |
| 47 | + config.NodeType = value |
| 48 | + } |
| 49 | + |
| 50 | + if value, exists := os.LookupEnv("WALLET_VERSION"); exists && value != "" { |
| 51 | + config.WalletVersion = value |
| 52 | + } |
| 53 | + |
| 54 | + if value, exists := os.LookupEnv("RPC_HOST"); exists && value != "" { |
| 55 | + config.RPCConfig.Host = value |
| 56 | + } |
| 57 | + |
| 58 | + if value, exists := os.LookupEnv("RPC_PORT"); exists && value != "" { |
| 59 | + config.RPCConfig.Port = value |
| 60 | + } |
| 61 | + |
| 62 | + if value, exists := os.LookupEnv("RPC_USER"); exists && value != "" { |
| 63 | + config.RPCConfig.RPCUser = value |
| 64 | + } |
| 65 | + |
| 66 | + if value, exists := os.LookupEnv("RPC_PASSWORD"); exists && value != "" { |
| 67 | + config.RPCConfig.RPCPassword = value |
| 68 | + } |
| 69 | + |
| 70 | + if value, exists := os.LookupEnv("RPC_IS_HTTPS"); exists && value != "" { |
| 71 | + isHTTPS, err := strconv.ParseBool(value) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + config.RPCConfig.IsHTTPS = isHTTPS |
| 76 | + } |
| 77 | + |
| 78 | + if value, exists := os.LookupEnv("SERVER_PING_TIME"); exists && value != "" { |
| 79 | + duration, err := time.ParseDuration(value) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
39 | 82 | } |
| 83 | + config.ServerPingTime = duration |
40 | 84 | } |
| 85 | + |
| 86 | + return config, nil |
41 | 87 | } |
0 commit comments