-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
59 lines (50 loc) · 1.39 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
package gofast
import "time"
type Config struct {
// fasthttp client configurations
Name string
NoDefaultUserAgentHeader bool
ReadTimeout time.Duration
WriteTimeout time.Duration
// RequestEncoder encode request before send
RequestEncoder RequestEncoder
// ResponseDecoder decode response after send
ResponseDecoder ResponseDecoder
// ErrorHandler handle the status code without 2xx
ErrorHandler ErrorHandler
}
// ConfigDefault is the default config
var ConfigDefault = Config{
Name: "gofast",
NoDefaultUserAgentHeader: false,
ReadTimeout: 6 * time.Second,
WriteTimeout: 6 * time.Second,
RequestEncoder: JSONEncoder,
ResponseDecoder: JSONDecoder,
ErrorHandler: defaultErrorHandler,
}
func configDefault(config ...Config) Config {
if len(config) < 1 {
return ConfigDefault
}
cfg := config[0]
if cfg.Name == "" {
cfg.Name = ConfigDefault.Name
}
if cfg.ReadTimeout == 0 {
cfg.ReadTimeout = ConfigDefault.ReadTimeout
}
if cfg.WriteTimeout == 0 {
cfg.WriteTimeout = ConfigDefault.WriteTimeout
}
if cfg.RequestEncoder == nil {
cfg.RequestEncoder = ConfigDefault.RequestEncoder
}
if cfg.ResponseDecoder == nil {
cfg.ResponseDecoder = ConfigDefault.ResponseDecoder
}
if cfg.ErrorHandler == nil {
cfg.ErrorHandler = ConfigDefault.ErrorHandler
}
return cfg
}