forked from yeqown/fasthttp-reverse-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
45 lines (37 loc) · 1.05 KB
/
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
package main
import (
"strings"
"time"
"github.com/yeqown/log"
"github.com/valyala/fasthttp"
proxy "github.com/yeqown/fasthttp-reverse-proxy"
)
var (
proxyServer = proxy.NewReverseProxy("localhost:8080", proxy.WithTimeout(5*time.Second))
proxyServer2 = proxy.NewReverseProxy("api-js.mixpanel.com")
proxyServer3 = proxy.NewReverseProxy("baidu.com")
)
// ProxyHandler ... fasthttp.RequestHandler func
func ProxyHandler(ctx *fasthttp.RequestCtx) {
requestURI := string(ctx.RequestURI())
log.Info("requestURI=", requestURI)
if strings.HasPrefix(requestURI, "/local") {
// "/local" path proxy to localhost
arr := strings.Split(requestURI, "?")
if len(arr) > 1 {
arr = append([]string{"/foo"}, arr[1:]...)
requestURI = strings.Join(arr, "?")
}
ctx.Request.SetRequestURI(requestURI)
proxyServer.ServeHTTP(ctx)
} else if strings.HasPrefix(requestURI, "/baidu") {
proxyServer3.ServeHTTP(ctx)
} else {
proxyServer2.ServeHTTP(ctx)
}
}
func main() {
if err := fasthttp.ListenAndServe(":8081", ProxyHandler); err != nil {
log.Fatal(err)
}
}