reverse http proxy hander based on fasthttp.
-
proxy client has
pool
supported -
faster than golang standard
httputil.ReverseProxy
-
simple warpper of
fasthttp.HostClient
-
websocket proxy
-
support balance distibute based
rounddobin
import (
"log"
"github.com/valyala/fasthttp"
proxy "github.com/gophemt/fasthttp-reverse-proxy"
)
var (
proxyServer = proxy.NewReverseProxy("localhost:8080")
// use with balancer
// weights = map[string]proxy.Weight{
// "localhost:8080": 20,
// "localhost:8081": 30,
// "localhost:8082": 50,
// }
// proxyServer = proxy.NewReverseProxy("", proxy.WithBalancer(weights))
)
// ProxyHandler ... fasthttp.RequestHandler func
func ProxyHandler(ctx *fasthttp.RequestCtx) {
// all proxy to localhost
proxyServer.ServeHTTP(ctx)
}
func main() {
if err := fasthttp.ListenAndServe(":8081", ProxyHandler); err != nil {
log.Fatal(err)
}
}
import (
"log"
"text/template"
"github.com/valyala/fasthttp"
proxy "github.com/gophemt/fasthttp-reverse-proxy"
)
var (
proxyServer = proxy.NewWSReverseProxy("localhost:8080", "/echo")
)
// ProxyHandler ... fasthttp.RequestHandler func
func ProxyHandler(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/":
proxyServer.ServeHTTP(ctx)
default:
ctx.Error("Unsupported path", fasthttp.StatusNotFound)
}
}
func main() {
if err := fasthttp.ListenAndServe(":8081", ProxyHandler); err != nil {
log.Fatal(err)
}
}