Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yeqown/fasthttp-reverse-proxy
reverse http / websocket proxy based on fasthttp
https://github.com/yeqown/fasthttp-reverse-proxy
fasthttp go http-proxy lib reverse-proxy websocket-proxy
Last synced: 5 days ago
JSON representation
reverse http / websocket proxy based on fasthttp
- Host: GitHub
- URL: https://github.com/yeqown/fasthttp-reverse-proxy
- Owner: yeqown
- License: mit
- Created: 2018-11-08T03:31:05.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-04T03:31:50.000Z (8 months ago)
- Last Synced: 2024-12-21T15:06:37.357Z (12 days ago)
- Topics: fasthttp, go, http-proxy, lib, reverse-proxy, websocket-proxy
- Language: Go
- Homepage:
- Size: 704 KB
- Stars: 217
- Watchers: 8
- Forks: 56
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fasthttp-reverse-proxy
![](https://img.shields.io/badge/LICENSE-MIT-blue.svg) [![Go Report Card](https://goreportcard.com/badge/github.com/yeqown/fasthttp-reverse-proxy/v2)](https://goreportcard.com/report/github.com/yeqown/fasthttp-reverse-proxy/v2) [![GoReportCard](https://godoc.org/github.com/yeqown/fasthttp-reverse-proxy/v2?status.svg)](https://godoc.org/github.com/yeqown/fasthttp-reverse-proxy/v2)reverse http proxy handler based on fasthttp.
## Features
- [x] `HTTP` reverse proxy based [fasthttp](https://github.com/valyala/fasthttp)
- [x] it's faster than golang standard `httputil.ReverseProxy` library.
- [x] implemented by `fasthttp.HostClient`
- [x] support balance distribute based `rounddobin`
- [x] `HostClient` object pool with an overlay of fasthttp connection pool.* [x] `WebSocket` reverse proxy.
## Get started
#### [HTTP (with balancer option)](./examples/fasthttp-reverse-proxy-with-bla/proxy.go)
```go
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](./examples/ws-fasthttp-reverse-proxy/README.md)
```go
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"),
// [OPTIONAL]: you can override path from `WithURL_OptionWS`
// by providing WithDynamicPath_OptionWS.
proxy.WithDynamicPath_OptionWS(true, proxy.DefaultOverrideHeader),
)
if err != nil {
panic(err)
}
})switch string(ctx.Path()) {
case "/echo":
// [OPTIONAL]: you can override path from `WithURL_OptionWS`
// by providing proxy.DefaultOverrideHeader (or any custom) header
// ctx.Request.Header.Set(proxy.DefaultOverrideHeader, "/real_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
* [HTTP reverse proxy](./examples/fasthttp-reverse-proxy/proxy.go)
* [HTTP reverse proxy with object pool](./examples/fasthttp-reverse-proxy-with-pool/pool.go)
* [Websocket reverse proxy](./examples/ws-fasthttp-reverse-proxy)## Contrast
* [HTTP benchmark](./docs/http-benchmark.md)
* [Websocket benchmark](./docs/ws-benchmark.md)## References
* [fasthttp](https://github.com/valyala/fasthttp)
* [standard httputil.ReverseProxy](https://golang.org/pkg/net/http/httputil/#ReverseProxy)
* [fasthttp/websocket](https://github.com/fasthttp/websocket)
* [koding/websocketproxy](https://github.com/koding/websocketproxy)## Thanks