goproxy

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2015 License: MIT Imports: 10 Imported by: 1

README

goproxy

GoProxy is a ReverseProxy / LoadBalancer helper for Golang

Example

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/creack/goproxy"
	"github.com/creack/goproxy/registry"
)

// ServiceRegistry is a local registry of services/versions
var ServiceRegistry = registry.DefaultRegistry{
	"service1": {
		"v1": {
			"localhost:9091",
			"localhost:9092",
		},
	},
}

func main() {
	http.HandleFunc("/", goproxy.NewMultipleHostReverseProxy(ServiceRegistry))
	http.HandleFunc("/health", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "%v\n", ServiceRegistry)
	})
	println("ready")
	log.Fatal(http.ListenAndServe(":9090", nil))
}

Limitations

Because we control only the connection, we can't have different http routes accross the same service. For the same reason, we can't have both HTTP and HTTPS for the same service. For now, this load balancer only supports HTTP.

Documentation

Overview

Package goproxy is a LoadBalancer based on httputil.ReverseProxy.

ExtractNameVersion and LoadBalance can be overridden in order to customize the behavior.

Example
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/creack/goproxy/registry"
)

// ServiceRegistry is a local registry of services/versions
var ServiceRegistry = registry.DefaultRegistry{
	"service1": {
		"v1": {
			"localhost:9091",
			"localhost:9092",
		},
	},
}

func main() {
	http.HandleFunc("/", NewMultipleHostReverseProxy(ServiceRegistry))
	http.HandleFunc("/health", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "%v\n", ServiceRegistry)
	})
	println("ready")
	log.Fatal(http.ListenAndServe(":9090", nil))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidService = errors.New("invalid service/version")
)

Common errors

View Source
var ExtractNameVersion = extractNameVersion

ExtractNameVersion is called to lookup the service name / version from the requested URL. It should update the URL's Path to reflect the target expectation.

View Source
var LoadBalance = loadBalance

LoadBalance is the default balancer which will use a random endpoint for the given service name/version.

Functions

func NewMultipleHostReverseProxy

func NewMultipleHostReverseProxy(reg registry.Registry) http.HandlerFunc

NewMultipleHostReverseProxy creates a reverse proxy handler that will randomly select a host from the passed `targets`

Types

This section is empty.

Directories

Path Synopsis
Package registry defines the Registry interface which can be used with goproxy.
Package registry defines the Registry interface which can be used with goproxy.

Jump to

Keyboard shortcuts

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