Skip to content

Commit ac32ff5

Browse files
committed
feat: fix logger output; add debug option
1 parent d23fedf commit ac32ff5

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

logger.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ type __Logger interface {
1111
type nopLogger struct{}
1212

1313
func (n *nopLogger) Printf(format string, args ...interface{}) {
14-
// if format not end with '/n', then append it
15-
if format[len(format)-1] != 0x0a {
16-
format += "/n"
14+
// if format not end with '\n', then append it
15+
if format[len(format)-1] != '\n' {
16+
format += "\n"
1717
}
1818

1919
fmt.Printf(format, args...)

reverseproxy.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,18 @@ func (p *ReverseProxy) init() error {
5858

5959
if p.opt.openBalance {
6060
// config balancer
61-
p.clients = make([]*fasthttp.HostClient, len(p.opt.addresses))
61+
p.clients = make([]*fasthttp.HostClient, 0, len(p.opt.addresses))
6262
p.bla = NewBalancer(p.opt.weights)
6363

64-
for idx, addr := range p.opt.addresses {
65-
p.clients[idx] = &fasthttp.HostClient{
64+
for _, addr := range p.opt.addresses {
65+
client := &fasthttp.HostClient{
6666
Addr: addr,
6767
Name: _fasthttpHostClientName,
6868
IsTLS: p.opt.tlsConfig != nil,
6969
TLSConfig: p.opt.tlsConfig,
7070
DisablePathNormalizing: p.opt.disablePathNormalizing,
7171
}
72+
p.clients = append(p.clients, client)
7273
}
7374

7475
return nil
@@ -133,10 +134,7 @@ func (p *ReverseProxy) ServeHTTP(ctx *fasthttp.RequestCtx) {
133134
}
134135

135136
c := p.getClient()
136-
137-
if p.opt.debug && p.opt.logger != nil {
138-
debugF(p.opt.debug, p.opt.logger, "rev request headers to proxy, addr = %s, headers = %s", c.Addr, req.Header.String())
139-
}
137+
debugF(p.opt.debug, p.opt.logger, "rev request headers to proxy, addr = %s, headers = %s", c.Addr, req.Header.String())
140138

141139
// assign the host to support virtual hosting, aka shared web hosting (one IP, multiple domains)
142140
req.SetHost(c.Addr)
@@ -154,9 +152,7 @@ func (p *ReverseProxy) ServeHTTP(ctx *fasthttp.RequestCtx) {
154152
}
155153

156154
// deal with response headers
157-
if p.opt.debug && p.opt.logger != nil {
158-
debugF(p.opt.debug, p.opt.logger, "rev response headers from proxy, addr = %s, headers = %s", c.Addr, res.Header.String())
159-
}
155+
debugF(p.opt.debug, p.opt.logger, "rev response headers from proxy, addr = %s, headers = %s", c.Addr, res.Header.String())
160156

161157
for _, h := range hopHeaders {
162158
res.Header.Del(h)

reverseproxy_option.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ func WithBalancer(addrWeights map[string]Weight) Option {
100100
})
101101
}
102102

103+
func WithDebug() Option {
104+
return newFuncBuildOption(func(o *buildOption) {
105+
o.debug = true
106+
})
107+
}
108+
103109
// WithTimeout specify the timeout of each request
104110
func WithTimeout(d time.Duration) Option {
105111
return newFuncBuildOption(func(o *buildOption) {

ws_reverseporxy_option.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ func WithURL_OptionWS(u string) OptionWS {
8282
})
8383
}
8484

85+
// WithDebug_OptionWS is used to enable debug mode.
86+
func WithDebug_OptionWS() OptionWS {
87+
return newFuncBuildOptionWS(func(o *buildOptionWS) {
88+
o.debug = true
89+
})
90+
}
91+
8592
// WithDialer_OptionWS use specified dialer
8693
func WithDialer_OptionWS(dialer *websocket.Dialer) OptionWS {
8794
return newFuncBuildOptionWS(func(o *buildOptionWS) {

0 commit comments

Comments
 (0)