-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws_proxy.go
47 lines (41 loc) · 1022 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
45
46
47
package main
import (
"log"
proxy "github.com/gophemt/fasthttp-reverse-proxy"
"github.com/valyala/fasthttp"
)
var (
proxyServer = proxy.NewWSReverseProxy("localhost:8080", "/echo")
)
// ProxyHandler ... fasthttp.RequestHandler func
func ProxyHandler(ctx *fasthttp.RequestCtx) {
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 {
log.Fatal(err)
}
}