forked from yeqown/fasthttp-reverse-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_proxy.go
44 lines (37 loc) · 829 Bytes
/
ws_proxy.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
package main
import (
"log"
"sync"
"github.com/valyala/fasthttp"
proxy "github.com/smantriplw/fasthttp-reverse-proxy/v2"
)
var (
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 "/":
fasthttp.ServeFileUncompressed(ctx, "./index.html")
default:
ctx.Error("Unsupported path", fasthttp.StatusNotFound)
}
}
func main() {
log.Println("serving on: 8081")
if err := fasthttp.ListenAndServe(":8081", ProxyHandler); err != nil {
log.Fatal(err)
}
}