Skip to content

gophemt/fasthttp-reverse-proxy

Repository files navigation

fasthttp-reverse-proxy

Go Report Card GoReportCard

reverse http proxy hander based on fasthttp.

features:

  • proxy client has pool supported

  • faster than golang standard httputil.ReverseProxy

  • simple warpper of fasthttp.HostClient

  • websocket proxy

  • support balance distibute based rounddobin

quick start

HTTP (with balancer option)

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)
	}
}

Websocket

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)
	}
}

usage

contrast

links:

Thanks

JetBrains

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published