forked from yeqown/fasthttp-reverse-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverseproxy_option.go
121 lines (100 loc) · 3.05 KB
/
reverseproxy_option.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
package proxy
import (
"crypto/tls"
"time"
)
// Option to define all options to reverse http proxy.
type Option interface {
apply(o *buildOption)
}
// buildOption contains all fields those are used in ReverseProxy.
type buildOption struct {
// logger to log some info
logger __Logger
// debug to open debug mode to log more info to logger
debug bool
// openBalance denote whether the balancer is configured or not.
openBalance bool
// weights weight of each upstream server. it would be empty if openBalance not true.
weights []W
// addresses all upstream server address. if openBalance not true,
// addresses will keep the only one upstream server address in addresses[0].
addresses []string
// tlsConfig is pointer to tls.Config, will be used if the upstream.
// need TLS handshake
tlsConfig *tls.Config
// timeout specify the timeout context with each request.
timeout time.Duration
// disablePathNormalizing disable path normalizing.
disablePathNormalizing bool
}
func defaultBuildOption() *buildOption {
return &buildOption{
logger: &nopLogger{},
debug: false,
openBalance: false,
weights: nil,
addresses: nil,
tlsConfig: nil,
timeout: 0,
disablePathNormalizing: false,
}
}
type funcBuildOption struct {
f func(o *buildOption)
}
func newFuncBuildOption(f func(o *buildOption)) funcBuildOption { return funcBuildOption{f: f} }
func (fb funcBuildOption) apply(o *buildOption) { fb.f(o) }
func WithTLSConfig(config *tls.Config) Option {
return newFuncBuildOption(func(o *buildOption) {
o.tlsConfig = config
})
}
// WithTLS build tls.Config with server certFile and keyFile.
// tlsConfig is nil as default
func WithTLS(certFile, keyFile string) Option {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
panic("" + err.Error())
}
return WithTLSConfig(&tls.Config{
Certificates: []tls.Certificate{cert},
})
}
// WithAddress generate address options
func WithAddress(addresses ...string) Option {
return newFuncBuildOption(func(o *buildOption) {
o.addresses = addresses
})
}
// WithBalancer generate balancer options
func WithBalancer(addrWeights map[string]Weight) Option {
weights := make([]W, 0, len(addrWeights))
addresses := make([]string, 0, len(addrWeights))
for k, v := range addrWeights {
weights = append(weights, v)
addresses = append(addresses, k)
}
return newFuncBuildOption(func(o *buildOption) {
o.addresses = addresses
o.openBalance = true
o.weights = weights
})
}
func WithDebug() Option {
return newFuncBuildOption(func(o *buildOption) {
o.debug = true
})
}
// WithTimeout specify the timeout of each request
func WithTimeout(d time.Duration) Option {
return newFuncBuildOption(func(o *buildOption) {
o.timeout = d
})
}
// WithDisablePathNormalizing sets whether disable path normalizing.
func WithDisablePathNormalizing(isDisablePathNormalizing bool) Option {
return newFuncBuildOption(func(o *buildOption) {
o.disablePathNormalizing = isDisablePathNormalizing
})
}