Skip to content

Commit 698272f

Browse files
committed
feat: Create load config module
1 parent a145214 commit 698272f

File tree

4 files changed

+75
-23
lines changed

4 files changed

+75
-23
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
install:
2+
go get "github.com/joho/godotenv"

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module bitcoin-api-interface
22

33
go 1.22.3
4+
5+
require github.com/joho/godotenv v1.5.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
2+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

src/utils/config.go

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,87 @@
11
package utils
22

33
import (
4-
"fmt"
4+
"os"
5+
"strconv"
56
"time"
6-
)
7-
8-
const (
9-
hostNodeBTC = "1212.154.54"
10-
loginNodeBTC = ""
11-
)
127

13-
var (
14-
urlBTC = fmt.Sprintf("https://%s:%s@127.0.0.1:8332", loginNodeBTC, hostNodeBTC)
8+
"github.com/joho/godotenv"
159
)
1610

1711
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
2217
}
2318

2419
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
2824
}
2925

3026
func DefaultConfig() *Config {
3127
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
3982
}
83+
config.ServerPingTime = duration
4084
}
85+
86+
return config, nil
4187
}

0 commit comments

Comments
 (0)