proxy

package module
v0.0.0-...-3b5a685 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2020 License: MIT Imports: 11 Imported by: 0

README

fasthttp-reverse-proxy

quick start

HTTP (with balancer option)
import (
	"log"

	"github.com/valyala/fasthttp"
	proxy "github.com/ac5tin/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/ac5tin/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

Documentation

Overview

Package proxy of reverse handler based fasthttp this lib ref to: Issue: https://github.com/valyala/fasthttp/issues/64 Code: https://golang.org/src/net/http/httputil/reverseproxy.go Pool Ref: https://github.com/fatih/pool/blob/master/channel.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultUpgrader specifies the parameters for upgrading an HTTP
	// connection to a WebSocket connection.
	DefaultUpgrader = &websocket.FastHTTPUpgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
	}

	// DefaultDialer is a dialer with all fields set to the default zero values.
	DefaultDialer = websocket.DefaultDialer
)

Functions

func SetProduction

func SetProduction()

SetProduction .

Types

type Factory

type Factory func(string) (*ReverseProxy, error)

Factory the generator to creat ReverseProxy

type IBalancer

type IBalancer interface {
	Distribute() int
}

IBalancer .

func NewBalancer

func NewBalancer(ws []W) IBalancer

NewBalancer . TODO:

type Option

type Option struct {
	OpenBalance bool
	Ws          []W
	Addrs       []string
}

Option . contains option field of ReverseProxy

func WithBalancer

func WithBalancer(addrWeights map[string]Weight) Option

WithBalancer .

type Pool

type Pool interface {
	// Get returns a new ReverseProxy from the pool.
	Get(string) (*ReverseProxy, error)

	// Put Reseting the ReverseProxy puts it back to the Pool.
	Put(*ReverseProxy) error

	// Close closes the pool and all its connections. After Close() the pool is
	// no longer usable.
	Close()

	// Len returns the current number of connections of the pool.
	Len() int
}

Pool interface ... this interface ref to: https://github.com/fatih/pool/blob/master/pool.go

func NewChanPool

func NewChanPool(initialCap, maxCap int, factory Factory) (Pool, error)

NewChanPool to new a pool with some params

type Proxier

type Proxier interface {
	ServeHTTP(ctx *fasthttp.RequestCtx)
	// ?
	SetClient(addr string) Proxier

	// Reset .
	Reset()

	// Close .
	Close()
}

Proxier can be HTTP or WebSocket proxier TODO:

type ReverseProxy

type ReverseProxy struct {
	// contains filtered or unexported fields
}

ReverseProxy reverse handler using fasthttp.HostClient TODO: support https config

func NewReverseProxy

func NewReverseProxy(oldAddr string, opts ...Option) *ReverseProxy

NewReverseProxy create an ReverseProxy with options

func (*ReverseProxy) Close

func (p *ReverseProxy) Close()

Close ... clear and release

func (*ReverseProxy) Reset

func (p *ReverseProxy) Reset()

Reset ...

func (*ReverseProxy) ServeHTTP

func (p *ReverseProxy) ServeHTTP(ctx *fasthttp.RequestCtx)

ServeHTTP ReverseProxy to serve ref to: https://golang.org/src/net/http/httputil/reverseproxy.go#L169

func (*ReverseProxy) SetClient

func (p *ReverseProxy) SetClient(addr string) *ReverseProxy

SetClient ...

type W

type W interface {
	Weight() int
}

W

type WSReverseProxy

type WSReverseProxy struct {

	// Upgrader specifies the parameters for upgrading a incoming HTTP
	// connection to a WebSocket connection. If nil, DefaultUpgrader is used.
	Upgrader *websocket.FastHTTPUpgrader

	//  Dialer contains options for connecting to the backend WebSocket server.
	//  If nil, DefaultDialer is used.
	Dialer *websocket.Dialer
	// contains filtered or unexported fields
}

WSReverseProxy . refer to https://github.com/koding/websocketproxy

func NewWSReverseProxy

func NewWSReverseProxy(host, path string) *WSReverseProxy

NewWSReverseProxy .

func (*WSReverseProxy) ServeHTTP

func (w *WSReverseProxy) ServeHTTP(ctx *fasthttp.RequestCtx)

ServeHTTP WSReverseProxy to serve

type Weight

type Weight uint

Weight .

func (Weight) Weight

func (w Weight) Weight() int

Jump to

Keyboard shortcuts

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