roxy

package module
v0.0.0-...-2b488f0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2015 License: BSD-3-Clause Imports: 10 Imported by: 0

README

Roxy Build GoDoc

A lightweight proxy with a familiar middleware implementation that allows you to modify HTTP requests and responses between clients and servers. Once the project is a little more stable there will be roxy-contribs with user contributed middleware.

Usage

Since things are in the alpha stage and quickly changing the best way to run a Roxy server is to build from source. Use at your own risk!

Start the example proxy which enables CORS and logging middleware:

git clone git@github.com:montanaflynn/roxy.git
cd roxy
go build
go run examples/main.go

Send a request:

curl -i localhost:8080 -H "host: anonfunction.com"
Examples

Enable CORS and JSON Logging:

package main

import (
	"net/http"
	"log"
	"time"
	"github.com/montanaflynn/roxy"
)

func main() {

	rp := roxy.Proxy()
	rp.AddMiddleware(roxy.Cors)
	rp.AddMiddleware(roxy.ConsoleLog)

	s := &http.Server{
		Addr:         ":8080",
		Handler:      rp,
		ReadTimeout:  30 * time.Second,
		WriteTimeout: 30 * time.Second,
	}

	log.Fatal(s.ListenAndServe())

}

Tips

You can set your system or browser or even twitter app to use a proxy like Roxy. Instructions vary but for example on a mac I found networksetup to be very useful.

First start Roxy on port 8080.

git clone git@github.com:montanaflynn/roxy.git
cd roxy
go build
go run examples/main.go

Then enable the system wide proxy to go through Roxy.

networksetup -setwebproxy Wi-Fi localhost 8080

You can replace Wi-Fi with any network connection which you can find with networksetup -listnetworkserviceorder, I always recommend taking a gander at the man page for any new commands (man networksetup).

When you want to turn it off it's this simple:

networksetup -setwebproxystate Wi-Fi off
Roadmap
  • Write unit tests for everything
  • Create flags for configuration
  • Integrate with etcd or consul
  • Allow for graceful restarts

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConsoleLog

func ConsoleLog(request *http.Request, response *http.Response)

func Cors

func Cors(request *http.Request, response *http.Response)

func JsonLog

func JsonLog(request *http.Request, response *http.Response)

Types

type MiddlewareFunc

type MiddlewareFunc func(*http.Request, *http.Response)

MiddlewareFunc is a function that is called to process a proxy response Since it has handle to the response object, it can manipulate the content

type ReverseProxy

type ReverseProxy struct {
	// Director must be a function which modifies
	// the request into a new request to be sent
	// using Transport. Its response is then copied
	// back to the original client unmodified.
	Director func(*http.Request)

	// Middleware must be an array of functions which modify
	// the response before the body is written
	Middleware []MiddlewareFunc

	// The transport used to perform proxy requests.
	// If nil, http.DefaultTransport is used.
	Transport http.RoundTripper

	// FlushInterval specifies the flush interval
	// to flush to the client while copying the
	// response body.
	// If zero, no periodic flushing is done.
	FlushInterval time.Duration

	// ErrorLog specifies an optional logger for errors
	// that occur when attempting to proxy the request.
	// If nil, logging goes to os.Stderr via the log package's
	// standard logger.
	ErrorLog *log.Logger
}

ReverseProxy is an HTTP Handler that takes an incoming request and sends it to another server, proxying the response back to the client.

func Proxy

func Proxy() *ReverseProxy

Create our roxy proxy with options

func (*ReverseProxy) AddMiddleware

func (p *ReverseProxy) AddMiddleware(m MiddlewareFunc)

func (*ReverseProxy) ServeHTTP

func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type Transport

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

Wrap our roxy transport up all nice

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(request *http.Request) (*http.Response, error)

Implements the round trip function

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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