libra

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: MIT Imports: 13 Imported by: 2

README

libra is a dynamic reverse proxy / load balancer

Build Status Go Report Card GoDoc codecov

English document中文文档

Feature

  • dynamic and multiple reverse proxy server
  • dynamic change origin server addr
  • dynamic change response header
  • rigorous unit testing

You can use this package build a dynamic reverse proxy server faster, now it has three load balance algorithms, random, roundrobin, wroundrobin (round robin with weight)

Getting Started

Installation

To install this package, you need to install Go and setup your Go workspace on your computer. The simplest way to install the library is to run:

go get github.com/zhuCheer/libra

run example

Change directory to libra package and run the example.go, you can start a reverse proxy.

> cd ../src/github.com/zhuCheer/libra/example
> go run example.go

Now,you can open browser to http://127.0.0.1:5000,you will see the reverse proxy, it running with round robin balance to http://127.0.0.1:5001, http://127.0.0.1:5002 and http://127.0.0.1:5003 http server.

How to use
import "github.com/zhuCheer/libra"

    
// create a new reverse proxy,input three params bind ip:port, custom response header;
// then register a site, fill in domain name, balancer algorithm and origin url scheme;
// it has three balancer algorithm random,roundrobin,wroundrobin(round robin with weight)
var srv = libra.NewHttpProxySrv("127.0.0.1:5000", nil)
srv.RegistSite("www.yourappdomain.com", "roundrobin", "http")

// add target domain and ip:port
srv.AddAddr("www.yourappdomain.com", "127.0.0.1:5001", 1)
srv.AddAddr("www.yourappdomain.com", "127.0.0.1:5001", 2)


// start reverse proxy server
srv.Start()
Principles
  • The reverse proxy serves as a gateway between users and your application origin server. In so doing it handles all policy management and traffic routing;
  • A reverse proxy operates by:
    1. Receiving a user connection request
    1. Completing a TCP three-way handshake, terminating the initial connection
    1. Connecting with the origin server and forwarding the original request

image

Functions

import "github.com/zhuCheer/libra"
var srv = libra.NewHttpProxySrv("127.0.0.1:5000", nil)
srv.RegistSite("www.yourappdomain.com", "roundrobin", "http")


// set response header
srv.ResetCustomHeader(map[string]string{"X-LIBRA": "the smart ReverseProxy"})

// change balance algorithm
srv.ChangeLoadType("www.yourappdomain.com", "random")


// add origin server addr, dynamic change without restarting
srv.AddAddr("www.yourappdomain.com","192.168.1.100:8081", 1)

// delete origin server addr, dynamic change without restarting
srv.DelAddr("www.yourappdomain.com","192.168.1.100:8081")

Contributors

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDefaultPage common error page template
	ErrDefaultPage = `` /* 435-byte string literal not displayed */

)
View Source
var (
	Logger = logger.NoopLogger{}
)

Common variable.

Functions

This section is empty.

Types

type ProxySrv

type ProxySrv struct {
	ProxyAddr string
	// contains filtered or unexported fields
}

ProxySrv Proxy server node struct

func NewHttpProxySrv

func NewHttpProxySrv(addr string, header map[string]string) *ProxySrv

NewHttpProxySrv new http reverse proxy

func (*ProxySrv) AddAddr added in v0.0.2

func (p *ProxySrv) AddAddr(domain string, addr string, weight uint32) *ProxySrv

AddAddr add addr quick func

func (*ProxySrv) ChangeLoadType

func (p *ProxySrv) ChangeLoadType(domain, loadType string)

ChangeLoadType change balancer loadType

func (*ProxySrv) DelAddr added in v0.0.2

func (p *ProxySrv) DelAddr(domain string, addr string)

AddAddr add addr quick func

func (*ProxySrv) FlushProxy added in v0.0.2

func (p *ProxySrv) FlushProxy(domain string)

Flush Flush proxy by domain

func (*ProxySrv) GetSiteInfo added in v0.0.2

func (p *ProxySrv) GetSiteInfo(domain string) (*balancer.RegistNode, error)

GetSiteInfo get balancer GetSiteInfo func

func (*ProxySrv) RegistSite added in v0.0.2

func (p *ProxySrv) RegistSite(domain, loadType, scheme string) *ProxySrv

RegistSite register a site

func (*ProxySrv) ResetCustomHeader

func (p *ProxySrv) ResetCustomHeader(header map[string]string)

ResetCustomHeader reset custom header

func (*ProxySrv) SetLoggerLevel added in v0.0.2

func (p *ProxySrv) SetLoggerLevel(level string)

Start http proxy server

func (*ProxySrv) Start

func (p *ProxySrv) Start() error

Start http proxy server

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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