Skip to content

Commit

Permalink
docs(example): upgrade ws reverseproxy example codes
Browse files Browse the repository at this point in the history
  • Loading branch information
yeqown committed Jun 1, 2021
1 parent 4a03f2f commit 9bc7ce5
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions examples/ws-fasthttp-reverse-proxy/ws_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,40 @@ package main

import (
"log"
"sync"

"github.com/valyala/fasthttp"

proxy "github.com/yeqown/fasthttp-reverse-proxy/v2"
)

var (
proxyServer = proxy.NewWSReverseProxy("localhost:8080", "/echo")
proxyServer *proxy.WSReverseProxy
once sync.Once
)

// ProxyHandler ... fasthttp.RequestHandler func
func ProxyHandler(ctx *fasthttp.RequestCtx) {
once.Do(func() {
var err error
proxyServer, err = proxy.NewWSReverseProxyWith(
proxy.WithURL_OptionWS("ws://localhost:8080/echo"),
)
if err != nil {
panic(err)
}
})

switch string(ctx.Path()) {
case "/echo":
proxyServer.ServeHTTP(ctx)
case "/":
// homeView(ctx)
fasthttp.ServeFileUncompressed(ctx, "./index.html")
default:
ctx.Error("Unsupported path", fasthttp.StatusNotFound)
}
}

// func homeView(ctx *fasthttp.RequestCtx) {
// ctx.SetContentType("text/html")
// fd, err := os.Open("./index.html")
// if err != nil {
// log.Printf("homeView err=%v", err)
// ctx.Write(p)
// }
// // buf := bytes.NewBuffer(nil)
// dat, err := ioutil.ReadAll(fd)
// if err != nil {
// log.Printf("homeView err=%v", err)
// }
// ctx.Write(dat)
// }

func main() {
log.Println("serving on: 8081")
if err := fasthttp.ListenAndServe(":8081", ProxyHandler); err != nil {
Expand Down

0 comments on commit 9bc7ce5

Please sign in to comment.