# README
Proxy - Make Reverse Proxy easier to use
Installation
To install the package, run:
go get -u github.com/go-zoox/proxy
Quick Start
package main
import (
"fmt"
"net/http"
"github.com/go-zoox/proxy"
)
func main() {
fmt.Println("Starting proxy at http://127.0.0.1:9999 ...")
http.ListenAndServe(":9999", proxy.New(&proxy.Config{
OnRequest: func(req *http.Request) error {
req.URL.Host = "127.0.0.1:8080"
return nil
},
}))
}
// visit http://127.0.0.1:9999/ip => http://127.0.0.1:8080/ip
// curl -v http://127.0.0.1:9999/ip
Best Practice
1. Single Host => All traffic to a single target with path
package main
import (
"fmt"
"net/http"
"github.com/go-zoox/proxy"
)
func main() {
target := "https://httpbin.org"
fmt.Println("Starting proxy at http://127.0.0.1:9999 ...")
http.ListenAndServe(":9999", proxy.NewSingleHost(target))
}
2. Single Host => All traffic to a single target with rewrite
package main
import (
"fmt"
"net/http"
"github.com/go-zoox/proxy"
"github.com/go-zoox/proxy/utils/rewriter"
)
func main() {
target := "https://httpbin.org"
fmt.Println("Starting proxy at http://127.0.0.1:9999 ...")
http.ListenAndServe(":9999", proxy.NewSingleHost(target, &proxy.SingleHostConfig{
Rewrites: rewriter.Rewriters{
{
From: "/api/ip",
To: "/ip",
},
{
From: "/api/headers",
To: "/headers",
},
{
From: "/api/v2/(.*)",
To: "/$1",
},
},
}))
}
3. Multiple Hosts => All traffic to multiple targets
package main
import (
"fmt"
"net/http"
"github.com/go-zoox/proxy"
)
func main() {
fmt.Println("Starting proxy at http://127.0.0.1:9999 ...")
http.ListenAndServe(":9999", proxy.NewMultiHosts(&proxy.MultiHostsConfig{
Routes: []proxy.MultiHostsRoute{
{
Host: "httpbin1.go-zoox.work",
Backend: proxy.MultiHostsRouteBackend{
ServiceProtocol: "https",
ServiceName: "httpbin.zcorky.com",
ServicePort: 443,
},
},
{
Host: "httpbin2.go-zoox.work",
Backend: proxy.MultiHostsRouteBackend{
ServiceProtocol: "https",
ServiceName: "httpbin.org",
ServicePort: 443,
},
},
},
}))
}
Inspiration
- Go httputil.ReverseProxy
License
GoZoox is released under the MIT License.
# Functions
CreateOnHTMLRewriteResponse create a function to rewrite html response.
CreateOnInjectScriptsResponse create a function to inject scripts.
New creates a new Proxy.
NewHTTPError creates a new HTTPError.
NewMultiHosts ...
NewSingleHost creates a new Single Host Proxy.
ParseHostPort parses host and port from a string in the form host[:port].
# Variables
Version is the version of this package.
# Structs
Config is the configuration for the Proxy.
HTTPError is an error that wraps an HTTP status code.
MultiHostsConfig ...
MultiHostsRoute ...
MultiHostsRouteBackend ...
Proxy is a Powerful HTTP Proxy, inspired by Go Reverse Proxy.
SingleHostConfig is the configuration for SingleTarget.
# Interfaces
A BufferPool is an interface for getting and returning temporary byte slices for use by io.CopyBuffer.