Skip to content

Commit 587bb6d

Browse files
committed
docs: update example codes
1 parent ac32ff5 commit 587bb6d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

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

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

33
import (
4+
"fmt"
45
"log"
56

67
"github.com/valyala/fasthttp"
@@ -15,7 +16,7 @@ var (
1516
"localhost:9092": 50,
1617
}
1718

18-
proxyServer, _ = proxy.NewReverseProxyWith(proxy.WithBalancer(weights))
19+
proxyServer, _ = proxy.NewReverseProxyWith(proxy.WithBalancer(weights), proxy.WithDebug())
1920
)
2021

2122
// ProxyHandler ... fasthttp.RequestHandler func
@@ -25,6 +26,7 @@ func ProxyHandler(ctx *fasthttp.RequestCtx) {
2526
}
2627

2728
func main() {
29+
fmt.Printf("listening on :8081\n")
2830
if err := fasthttp.ListenAndServe(":8081", ProxyHandler); err != nil {
2931
log.Fatal(err)
3032
}

examples/server.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,24 @@ var (
1414

1515
func main() {
1616
flag.Parse()
17+
addr := fmt.Sprintf(":%d", *port)
18+
1719
http.HandleFunc("/foo", func(w http.ResponseWriter, req *http.Request) {
20+
clientIP := req.Header.Get("X-Forwarded-For")
21+
fmt.Printf("got request from %s\n", clientIP)
22+
1823
_ = req.ParseForm()
1924
timeout, err := strconv.Atoi(req.FormValue("timeout"))
2025
if err == nil && timeout > 0 {
2126
fmt.Println("timeout=", timeout)
2227
time.Sleep(time.Duration(timeout) * time.Second)
2328
}
2429

25-
ip := req.RemoteAddr
2630
w.Header().Add("X-Test", "true")
27-
_, _ = fmt.Fprintf(w, "bar: %d, %s", 200, ip)
31+
_, _ = fmt.Fprintf(w, "response from %s", addr)
2832
})
2933

30-
addr := fmt.Sprintf(":%d", *port)
34+
fmt.Printf("listening on %s\n", addr)
3135
if err := http.ListenAndServe(addr, nil); err != nil {
3236
panic(err)
3337
}

0 commit comments

Comments
 (0)