Skip to content

Commit d23fedf

Browse files
committed
feat: update example codes
1 parent efff62d commit d23fedf

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

examples/fasthttp-reverse-proxy-with-bla/proxy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55

66
"github.com/valyala/fasthttp"
7+
78
proxy "github.com/yeqown/fasthttp-reverse-proxy/v2"
89
)
910

@@ -14,7 +15,7 @@ var (
1415
"localhost:9092": 50,
1516
}
1617

17-
proxyServer = proxy.NewReverseProxy("", proxy.WithBalancer(weights))
18+
proxyServer, _ = proxy.NewReverseProxyWith(proxy.WithBalancer(weights))
1819
)
1920

2021
// ProxyHandler ... fasthttp.RequestHandler func

examples/fasthttp-reverse-proxy-with-pool/pool.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"log"
55

66
"github.com/valyala/fasthttp"
7+
78
proxy "github.com/yeqown/fasthttp-reverse-proxy/v2"
89
)
910

1011
var (
1112
pool proxy.Pool
12-
err error
1313
)
1414

1515
// ProxyPoolHandler ...
@@ -25,14 +25,17 @@ func ProxyPoolHandler(ctx *fasthttp.RequestCtx) {
2525
}
2626

2727
func factory(hostAddr string) (*proxy.ReverseProxy, error) {
28-
p := proxy.NewReverseProxy(hostAddr)
29-
return p, nil
28+
return proxy.NewReverseProxyWith(proxy.WithAddress(hostAddr))
3029
}
3130

3231
func main() {
3332
initialCap, maxCap := 100, 1000
33+
var err error
3434
pool, err = proxy.NewChanPool(initialCap, maxCap, factory)
35-
if err := fasthttp.ListenAndServe(":8083", ProxyPoolHandler); err != nil {
35+
if err != nil {
36+
panic(err)
37+
}
38+
if err = fasthttp.ListenAndServe(":8083", ProxyPoolHandler); err != nil {
3639
panic(err)
3740
}
3841
}

examples/fasthttp-reverse-proxy/proxy.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package main
22

33
import (
4+
"log"
45
"strings"
56
"time"
67

7-
"github.com/yeqown/log"
8-
98
"github.com/valyala/fasthttp"
9+
1010
proxy "github.com/yeqown/fasthttp-reverse-proxy/v2"
1111
)
1212

1313
var (
14-
proxyServer = proxy.NewReverseProxy("localhost:8080", proxy.WithTimeout(5*time.Second))
15-
proxyServer2 = proxy.NewReverseProxy("api-js.mixpanel.com")
16-
proxyServer3 = proxy.NewReverseProxy("baidu.com")
14+
proxyServer, _ = proxy.NewReverseProxyWith(proxy.WithAddress("localhost:8080"), proxy.WithTimeout(5*time.Second))
15+
proxyServer2, _ = proxy.NewReverseProxyWith(proxy.WithAddress("api-js.mixpanel.com"))
16+
proxyServer3, _ = proxy.NewReverseProxyWith(proxy.WithAddress("baidu.com"))
1717
)
1818

1919
// ProxyHandler ... fasthttp.RequestHandler func
2020
func ProxyHandler(ctx *fasthttp.RequestCtx) {
2121
requestURI := string(ctx.RequestURI())
22-
log.Info("requestURI=", requestURI)
22+
log.Printf("requestURI=%s\n", requestURI)
2323

2424
if strings.HasPrefix(requestURI, "/local") {
2525
// "/local" path proxy to localhost

examples/https-reverse-proxy/proxy.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
package main
22

33
import (
4-
"github.com/yeqown/log"
4+
"log"
55

66
"github.com/valyala/fasthttp"
7+
78
proxy "github.com/yeqown/fasthttp-reverse-proxy/v2"
89
)
910

1011
var (
11-
proxyServer = proxy.NewReverseProxy("127.0.0.1:8080",
12-
proxy.WithTLS("./selfsigned.crt", "./selfsigned.key"))
12+
proxyServer, _ = proxy.NewReverseProxyWith(
13+
proxy.WithAddress("localhost:8080"),
14+
proxy.WithTLS("./selfsigned.crt", "./selfsigned.key"),
15+
)
1316
)
1417

1518
// ProxyHandler ... fasthttp.RequestHandler func
1619
func ProxyHandler(ctx *fasthttp.RequestCtx) {
1720
requestURI := string(ctx.RequestURI())
18-
log.Info("a request incoming, requestURI=", requestURI)
21+
log.Printf("a request incoming, requestURI=%s\n", requestURI)
1922
proxyServer.ServeHTTP(ctx)
2023
}
2124

0 commit comments

Comments
 (0)