revproxy

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2020 License: Apache-2.0 Imports: 15 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

revproxy

Apache License GoDoc Go Report Card Releases

revproxy provides simple and configurable reverse proxy.

features:

  • httputil.ReverseProxy base HTTP Proxy Server
  • Simple Programable configure
  • Port mapping Proxy
  • URL based Consistent Hashing Proxy

Download

Linux amd64 / Darwin amd64 binaries are available in Releases

Build

Build requires Go version 1.13+ installed.

$ go version

Run make pkg to Build and package for linux, darwin.

$ git clone https://github.com/octu0/revproxy
$ make pkg

Configure

Simple Proxy

To proxy a specific Path, use HandleFunc and Proxy functions

{{ HandleFunc "/ok" (Text 200 "OK") }}
{{ HandleFunc "/foobar1" (Proxy "http://targethost:8080/foobar2") }}
{{ HandleFunc "/" (Proxy "http://www.google.com/") }}

In this configuration, proxy is performed as below:

- http://localhost:8080/         => proxy http://www.google.com/
- http://localhost:8080/foobar1  => proxy http://targethost:8080/foobar2
- http://localhost:8080/ok       => response "OK"

Port mapping Proxy

using the built-in functions and the key/value values set with --value when revproxy is started, proxy specify the port number.

Specify HandlePrefix to make all paths under specific path to be proxy.

{{ with $path := "/port-balance/{id:[0-9]+}" -}}
  {{- with $url := "http://{{ hostport .BASE_IP .BASE_PORT .id }}/api/{{ .id }}" -}}
  {{ HandlePrefix $path (Proxy $url) }}
  {{- end -}}
{{- end }}

In this configuration, proxy is performed as below:

- http://localhost:8080/port-balance/123 => proxy http://localhost:8123/api/123
- http://localhost:8080/port-balance/456 => proxy http://localhost:8456/api/456
Example

Try launching it as below:

$ revproxy server --value BASE_IP=localhost --value BASE_PORT=8000 -t '{{ with $path := "/port-balance/{id:[0-9]+}" -}}
  {{- with $url := "http://{{ hostport .BASE_IP .BASE_PORT .id }}/api/{{ .id }}" -}}
  {{ HandlePrefix $path (Proxy $url) }}
  {{- end -}}
{{- end }}'

Destination server started as below:

# server1
$ revproxy server --port 8123 -t '{{ HandlePrefix "/api/123" (Text 200 "i am :8123/api/123") }}' &

# server2
$ revproxy server --port 8456 -t '{{ HandlePrefix "/api/456" (Text 200 "i am :8456/api/456") }}' &

Try to get the requests distributed.

$ curl -XGET localhost:8080/port-balance/123
i am :8123/api/123

$ curl -XGET localhost:8080/port-balance/456
i am :8456/api/456

Consistent Hashing Proxy

Proxy routing by path-based consistent hashing is defined as below:

{{ with $path := "/consistent-hashing/{key}" -}}
  {{- $url1 := "http://{{ .BASE_IP }}:8081/{{ .key }}/" -}}
  {{- $url2 := "http://{{ .BASE_IP }}:8082/{{ .key }}/" -}}
  {{- $url3 := "http://{{ .BASE_IP }}:8083/{{ .key }}/" -}}
  {{ HandleFunc $path (ProxyConsistent $url1 $url2 $url3) }}
{{- end }}

This will result in the following distribution.

- http://localhost:8080/consistent-hashing/a => proxy http://localhost:8081/a
- http://localhost:8080/consistent-hashing/b => proxy http://localhost:8082/b
- http://localhost:8080/consistent-hashing/c => proxy http://localhost:8083/c
- http://localhost:8080/consistent-hashing/b => proxy http://localhost:8081/b
Example

Try launching it as below:

### proxy server
$ revproxy server --value BASE_IP=localhost -t '{{ with $path := "/consistent-hashing/{key}" -}}
>   {{- $url1 := "http://{{ .BASE_IP }}:8081/{{ .key }}/" -}}
>   {{- $url2 := "http://{{ .BASE_IP }}:8082/{{ .key }}/" -}}
>   {{- $url3 := "http://{{ .BASE_IP }}:8083/{{ .key }}/" -}}
>   {{ HandleFunc $path (ProxyConsistent $url1 $url2 $url3) }}
> {{- end }}'

Destination server started as below:

# server1
$ revproxy server --port 8081 -t '{{ HandlePrefix "/" (Text 200 "i am 8081") }}' &

# server2
$ revproxy server --port 8082 -t '{{ HandlePrefix "/" (Text 200 "i am 8082") }}' &

# server2
$ revproxy server --port 8083 -t '{{ HandlePrefix "/" (Text 200 "i am 8083") }}' &

Try to get the requests distributed.

$ curl -XGET localhost:8080/consistent-hashing/a
i am 8081

$ curl -XGET localhost:8080/consistent-hashing/b
i am 8083

$ curl -XGET localhost:8080/consistent-hashing/c
i am 8082

$ curl -XGET localhost:8080/consistent-hashing/a
i am 8081

Help

NAME:
   revproxy server

USAGE:
   revproxy server [command options] [arguments...]

OPTIONS:
   --host value                      bind host (default: "[0.0.0.0]") [$REVPROXY_HOST]
   --port value                      bind port (default: 8080) [$REVPROXY_PORT]
   -t value, --template value        /path/to.tpl reverse proxy router template value or template file path [$REVPROXY_TEMPLATE]
   --header value                    allow headers
   --value value, -v value           specify variables to be applied to template, format KEY=Value (e.g. IP=10.16.0.2)
   --http-read-timeout value         http server read timeout(time-unit: second) (default: 10)
   --http-write-timeout value        http server write timeout(time-unit: second) (default: 10)
   --http-idle-timeout value         http server idle timeout(time-unit: second) (default: 30)
   --http-read-header-timeout value  http server read header timeout(time-unit: second) (default: 15)

License

Apache 2.0, see LICENSE file for details.

Documentation

Index

Constants

View Source
const (
	AppName string = "revproxy"
	Version string = "1.0.0"
	UA      string = AppName + "/" + Version
)
View Source
const DefaultConfigTemplate string = `` /* 634-byte string literal not displayed */

Variables

View Source
var (
	CommonFuncMap = template.FuncMap{
		"hostport": func(baseIP, basePort string, value string) string {
			port, _ := strconv.Atoi(basePort)
			v, _ := strconv.Atoi(value)
			return net.JoinHostPort(baseIP, strconv.Itoa(port+v))
		},
		"add": func(a, b string) string {
			i, _ := strconv.Atoi(a)
			j, _ := strconv.Atoi(b)
			return strconv.Itoa(i + j)
		},
		"sub": func(a, b string) string {
			i, _ := strconv.Atoi(a)
			j, _ := strconv.Atoi(b)
			return strconv.Itoa(i - j)
		},
	}
)

Functions

func AllowHeaders

func AllowHeaders(headers []string) serverOptFunc

func IdleTimeout

func IdleTimeout(dur time.Duration) serverOptFunc

func ListenAddr

func ListenAddr(addr string) serverOptFunc

func NewServer

func NewServer(funcs ...serverOptFunc) *server

func ReadHeaderTimeout

func ReadHeaderTimeout(dur time.Duration) serverOptFunc

func ReadTimeout

func ReadTimeout(dur time.Duration) serverOptFunc

func WriteTimeout

func WriteTimeout(dur time.Duration) serverOptFunc

Types

type Command

type Command struct {
	Success bool
	Type    string
	Handler func(http.ResponseWriter, *http.Request)
}

type KeyValue

type KeyValue map[string]string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址