-
Notifications
You must be signed in to change notification settings - Fork 39
/
config.go
160 lines (142 loc) · 3.75 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package node
import (
"encoding/json"
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/icon-project/goloop/common/log"
"github.com/icon-project/goloop/module"
"github.com/icon-project/goloop/server/jsonrpc"
)
const (
ChainConfigFileName = "config.json"
ChainGenesisZipFileName = "genesis.zip"
)
type StaticConfig struct {
// static
CliSocket string `json:"node_sock"` // relative path
P2PAddr string `json:"p2p"`
P2PListenAddr string `json:"p2p_listen"`
RPCAddr string `json:"rpc_addr"`
RPCDump bool `json:"rpc_dump"`
EESocket string `json:"ee_socket"`
Engines string `json:"engines"`
BackupDir string `json:"backup_dir"`
AuthSkipIfEmptyUsers bool `json:"auth_skip_if_empty_users,omitempty"`
NIDForP2P bool `json:"nid_for_p2p,omitempty"`
BaseDir string `json:"node_dir"`
FilePath string `json:"-"` // absolute path
// build info
BuildVersion string `json:"-"`
BuildTags string `json:"-"`
}
func (c *StaticConfig) ResolveAbsolute(targetPath string) string {
return ResolveAbsolute(c.FilePath, targetPath)
}
func (c *StaticConfig) ResolveRelative(targetPath string) string {
absPath, _ := filepath.Abs(targetPath)
base, _ := filepath.Abs(filepath.Dir(c.FilePath))
r, _ := filepath.Rel(base, absPath)
return r
}
func ResolveAbsolute(baseFile, targetPath string) string {
if filepath.IsAbs(targetPath) {
return targetPath
}
if baseFile == "" {
r, _ := filepath.Abs(targetPath)
return r
}
if !filepath.IsAbs(baseFile) {
baseFile, _ = filepath.Abs(baseFile)
}
return filepath.Clean(path.Join(filepath.Dir(baseFile), targetPath))
}
func (c *StaticConfig) SetFilePath(path string) string {
o := c.FilePath
c.FilePath, _ = filepath.Abs(path)
if c.BaseDir != "" {
c.BaseDir = c.ResolveRelative(ResolveAbsolute(o, c.BaseDir))
}
if c.CliSocket != "" {
c.CliSocket = c.ResolveRelative(ResolveAbsolute(o, c.CliSocket))
}
if c.EESocket != "" {
c.EESocket = c.ResolveRelative(ResolveAbsolute(o, c.EESocket))
}
if c.BackupDir != "" {
c.BackupDir = c.ResolveRelative(ResolveAbsolute(o, c.BackupDir))
}
return o
}
func (c *StaticConfig) FillEmpty(addr module.Address) {
if c.BaseDir == "" {
c.BaseDir = path.Join(".", ".chain", addr.String())
}
if c.BackupDir == "" {
c.BackupDir = path.Join(c.BaseDir, "backup")
}
if c.CliSocket == "" {
c.CliSocket = path.Join(c.BaseDir, "cli.sock")
}
if c.EESocket == "" {
c.EESocket = path.Join(c.BaseDir, "ee.sock")
}
}
func (c *StaticConfig) AbsBaseDir() string {
return c.ResolveAbsolute(c.BaseDir)
}
const (
DefaultEEInstances = 1
)
type RuntimeConfig struct {
EEInstances int `json:"eeInstances"`
RPCDefaultChannel string `json:"rpcDefaultChannel"`
RPCIncludeDebug bool `json:"rpcIncludeDebug"`
RPCBatchLimit int `json:"rpcBatchLimit"`
FilePath string `json:"-"` // absolute path
}
func (c *RuntimeConfig) load() error {
log.Println("load ", c.FilePath)
if _, err := os.Stat(c.FilePath); err != nil {
return err
}
b, err := ioutil.ReadFile(c.FilePath)
if err != nil {
log.Printf("%T %+v", err, err)
return err
}
if err = json.Unmarshal(b, c); err != nil {
return err
}
return nil
}
func (c *RuntimeConfig) save() error {
log.Println("save ", c.FilePath)
b, err := json.Marshal(c)
if err != nil {
return err
}
if err = ioutil.WriteFile(c.FilePath, b, 0644); err != nil {
return err
}
return err
}
func loadRuntimeConfig(baseDir string) (*RuntimeConfig, error) {
cfg := &RuntimeConfig{
EEInstances: DefaultEEInstances,
RPCBatchLimit: jsonrpc.DefaultBatchLimit,
FilePath: path.Join(baseDir, "rconfig.json"),
}
if err := cfg.load(); err != nil {
if os.IsNotExist(err) {
if err = cfg.save(); err != nil {
return nil, err
}
return cfg, nil
}
return nil, err
}
return cfg, nil
}