-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
249 lines (206 loc) · 8.45 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package sdk
import (
"fmt"
"github.com/flyleft/gprofile"
"os"
"strings"
)
var appConfig ApplicationConfig
var cssConfig Configuration
var cssLoggerConfig LoggerConf
//var GatewayTimeout = 60
//var Port int
//var MaxExecTime = 300
//var IsAFSExist bool
//var DownloadDir string
//var UploadDir string
//var UploadFieldModel string
//
//const DownloadFolder = "download/"
//const UploadFolder = "upload/"
//var ContentTypeMap = make(map[string]string, 20)
type Configuration struct {
// 链存服务API地址
ChainStorageApiEndpoint string `profile:"chainStorageApiEndpoint" profileDefault:"http://127.0.0.1:8821" json:"chainStorageApiEndpoint"`
// CAR文件工作目录
CarFileWorkPath string `profile:"carFileWorkPath" profileDefault:"./tmp/carfile" json:"carFileWorkPath"`
// CAR文件分片阈值
CarFileShardingThreshold int `profile:"carFileShardingThreshold" profileDefault:"46137344" json:"carFileShardingThreshold"`
// 链存服务API token
ChainStorageApiToken string `profile:"chainStorageApiToken" profileDefault:"" json:"chainStorageApiToken"`
// HTTP request user agent (K2请求需要)
HttpRequestUserAgent string `profile:"httpRequestUserAgent" profileDefault:"" json:"httpRequestUserAgent"`
// HTTP request user agent (K2请求需要)
HttpRequestOvertime int `profile:"httpRequestOvertime" profileDefault:"30" json:"httpRequestOvertime"`
// CAR version
CarVersion int `profile:"carVersion" profileDefault:"1" json:"carVersion"`
UseHTTPSProtocol bool `profile:"useHttpsProtocol" profileDefault:"true" json:"useHttpsProtocol"`
}
type LoggerConf struct {
LogPath string `profile:"logPath" profileDefault:"./logs" json:"logPath"`
Mode string `prfile:"mode" profileDefault:"release" json:"mode"`
Level string `prfile:"level" profileDefault:"info" json:"level"`
IsOutPutFile bool `profile:"isOutPutFile" profileDefault:"false" json:"isOutPutFile"`
MaxAgeDay int64 `profile:"maxAgeDay" profileDefault:"7" json:"maxAgeDay"`
RotationTime int64 `profile:"rotationTime" profileDefault:"1" json:"rotationTime"`
}
type ApplicationConfig struct {
Server Configuration `profile:"server"`
Logger LoggerConf `profile:"logger"`
}
func initConfig(config *ApplicationConfig) {
cssConfig = config.Server
cssLoggerConfig = config.Logger
//check chain-storage-api base address
if len(cssConfig.ChainStorageApiEndpoint) > 0 {
chainStorageAPIEndpoint := cssConfig.ChainStorageApiEndpoint
if !strings.HasPrefix(chainStorageAPIEndpoint, "http://") &&
!strings.HasPrefix(chainStorageAPIEndpoint, "https://") {
if cssConfig.UseHTTPSProtocol {
cssConfig.ChainStorageApiEndpoint = "https://" + chainStorageAPIEndpoint
} else {
cssConfig.ChainStorageApiEndpoint = "http://" + chainStorageAPIEndpoint
}
//fmt.Println("ERROR: invalid chain-storage-api endpoint in Configuration, chain-storage-api endpoint must be a valid http/https url, exiting")
//os.Exit(1)
}
if !strings.HasSuffix(chainStorageAPIEndpoint, "/") {
cssConfig.ChainStorageApiEndpoint += "/"
}
} else {
fmt.Println("ERROR: no chain-storage-api endpoint provided in Configuration, at least 1 valid http/https chain-storage-api endpoint must be given, exiting")
os.Exit(1)
}
if len(cssConfig.ChainStorageApiToken) == 0 {
fmt.Println("ERROR: invalid chain-storage-api token in Configuration, chain-storage-api token must not be empty")
os.Exit(1)
} else if !strings.HasPrefix(cssConfig.ChainStorageApiToken, "Bearer ") {
cssConfig.ChainStorageApiToken = "Bearer " + cssConfig.ChainStorageApiToken
}
//// CAR文件分片阈值,缺省10MB
//carFileShardingThreshold := cssConfig.CarFileShardingThreshold
//if carFileShardingThreshold <= 0 {
// cssConfig.CarFileShardingThreshold = 10485760
//}
// CAR文件分片阈值(固定44Mb)
cssConfig.CarFileShardingThreshold = 46137344
// CAR文件工作目录
carFileWorkPath := cssConfig.CarFileWorkPath
if len(carFileWorkPath) == 0 {
cssConfig.CarFileWorkPath = `./tmp/carfile`
}
// HTTP request user agent (K2请求需要)
httpRequestUserAgent := cssConfig.HttpRequestUserAgent
if len(httpRequestUserAgent) == 0 {
cssConfig.HttpRequestUserAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36`
}
// HTTP request overtime
httpRequestOvertime := cssConfig.HttpRequestOvertime
if httpRequestOvertime <= 0 {
cssConfig.HttpRequestOvertime = 30
}
// CAR version
carVersion := cssConfig.CarVersion
if carVersion <= 0 {
cssConfig.CarVersion = 1
}
}
func initConfigWithConfigFile(configFile string) {
//rand.Seed(time.Now().UnixNano())
//if len(configFile) == 0 {
// configFile = "./github.com/paradeum-team/chainstorage-sdk.yaml"
//}
if len(configFile) == 0 {
configFile = "./chainstorage-sdk.yaml"
}
config, err := gprofile.Profile(&ApplicationConfig{}, configFile, true)
if err != nil {
fmt.Errorf("Profile execute error", err)
}
appConfig = *config.(*ApplicationConfig)
cssConfig = config.(*ApplicationConfig).Server
cssLoggerConfig = config.(*ApplicationConfig).Logger
//dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
//if err != nil {
// log.Fatal(err)
// os.Exit(1)
//}
//check chain-storage-api base address
if len(cssConfig.ChainStorageApiEndpoint) > 0 {
chainStorageAPIEndpoint := cssConfig.ChainStorageApiEndpoint
if !strings.HasPrefix(chainStorageAPIEndpoint, "http://") &&
!strings.HasPrefix(chainStorageAPIEndpoint, "https://") {
if cssConfig.UseHTTPSProtocol {
cssConfig.ChainStorageApiEndpoint = "https://" + chainStorageAPIEndpoint
} else {
cssConfig.ChainStorageApiEndpoint = "http://" + chainStorageAPIEndpoint
}
//fmt.Println("ERROR: invalid chain-storage-api endpoint in Configuration, chain-storage-api endpoint must be a valid http/https url, exiting")
//os.Exit(1)
}
if !strings.HasSuffix(chainStorageAPIEndpoint, "/") {
cssConfig.ChainStorageApiEndpoint += "/"
}
} else {
fmt.Println("ERROR: no chain-storage-api endpoint provided in Configuration, at least 1 valid http/https chain-storage-api endpoint must be given, exiting")
os.Exit(1)
}
if len(cssConfig.ChainStorageApiToken) == 0 {
fmt.Println("ERROR: invalid chain-storage-api token in Configuration file, chain-storage-api token must not be empty")
os.Exit(1)
} else if !strings.HasPrefix(cssConfig.ChainStorageApiToken, "Bearer ") {
cssConfig.ChainStorageApiToken = "Bearer " + cssConfig.ChainStorageApiToken
}
//if _, err := os.Stat(filepath.Join(Config.AbsAFSDir, Config.AFSProgram)); os.IsExist(err) {
// IsAFSExist = false
//} else if err != nil {
// IsAFSExist = false
//}
////check port
//if Config.Port > 1023 && Config.Port <= 65535 {
// Port = Config.Port
//} else {
// fmt.Printf("WARNING: invalid port number(port) in Configuration file, using default:%s\n", Port)
//}
}
func InitConfigWithDefault() {
//rand.Seed(time.Now().UnixNano())
config, err := gprofile.Profile(&ApplicationConfig{}, "./chainstorage-sdk.yaml", true)
if err != nil {
fmt.Errorf("Profile execute error", err)
}
appConfig = *config.(*ApplicationConfig)
cssConfig = config.(*ApplicationConfig).Server
cssLoggerConfig = config.(*ApplicationConfig).Logger
//dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
//if err != nil {
// log.Fatal(err)
// os.Exit(1)
//}
//check chain-storage-api base address
if len(cssConfig.ChainStorageApiEndpoint) > 0 {
chainStorageAPIEndpoint := cssConfig.ChainStorageApiEndpoint
if !strings.HasPrefix(chainStorageAPIEndpoint, "http://") &&
!strings.HasPrefix(chainStorageAPIEndpoint, "https://") {
if cssConfig.UseHTTPSProtocol {
cssConfig.ChainStorageApiEndpoint = "https://" + chainStorageAPIEndpoint
} else {
cssConfig.ChainStorageApiEndpoint = "http://" + chainStorageAPIEndpoint
}
//fmt.Println("ERROR: invalid chain-storage-api endpoint in Configuration, chain-storage-api endpoint must be a valid http/https url, exiting")
//os.Exit(1)
}
if !strings.HasSuffix(chainStorageAPIEndpoint, "/") {
cssConfig.ChainStorageApiEndpoint += "/"
}
} else {
fmt.Println("ERROR: no chain-storage-api endpoint provided in Configuration, at least 1 valid http/https chain-storage-api endpoint must be given, exiting")
os.Exit(1)
}
if len(cssConfig.ChainStorageApiToken) == 0 {
fmt.Println("ERROR: invalid chain-storage-api token in Configuration file, chain-storage-api token must not be empty")
os.Exit(1)
} else if !strings.HasPrefix(cssConfig.ChainStorageApiToken, "Bearer ") {
cssConfig.ChainStorageApiToken = "Bearer " + cssConfig.ChainStorageApiToken
}
}