# README
fasthttp-reverse-proxy
reverse http proxy handler based on fasthttp.
Features
-
HTTP
reverse proxy based fasthttp- it's faster than golang standard
httputil.ReverseProxy
library. - implemented by
fasthttp.HostClient
- support balance distribute based
rounddobin
-
HostClient
object pool with an overlay of fasthttp connection pool.
- it's faster than golang standard
-
WebSocket
reverse proxy.
Get started
HTTP (with balancer option)
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)
}
}
Websocket
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)
}
}
Usages
Contrast
References
Thanks

# Functions
NewBalancer constructs a IBalancer instance which implements roundrobin algorithm.
NewChanPool to new a pool with some params.
NewReverseProxy create an ReverseProxy with options.
NewWSReverseProxy constructs a new WSReverseProxy to serve requests.
NewWSReverseProxyWith constructs a new WSReverseProxy with options.
SetProduction .
WithBalancer generate balancer options.
WithDialer_OptionWS use specified dialer.
WithForwardHeadersHandlers_OptionWS allows users to customize forward headers.
WithTimeout specify the timeout of each request.
WithTLS build tls.Config with server certFile and keyFile.
No description provided by the author
WithUpgrader_OptionWS use specified upgrader.
WithURL_OptionWS specify the url to backend websocket server.
# Variables
DefaultDialer is a dialer with all fields set to the default zero values.
DefaultUpgrader specifies the parameters for upgrading an HTTP connection to a WebSocket connection.