replay

package module
v0.0.0-...-1226a28 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2016 License: MIT Imports: 6 Imported by: 0

README

replay Build Status GoDoc API Coverage Status Go Report Card

Replay HTTP traffic to multiple servers easily.

Installation

go get -u gopkg.in/vinxi/replay.v0

API

See godoc reference.

Examples

Replay to multiple servers
package main

import (
  "fmt"
  "gopkg.in/vinxi/replay.v0"
  "gopkg.in/vinxi/vinxi.v0"
  "net/http"
)

func main() {
  vs := vinxi.NewServer(vinxi.ServerOptions{Host: "localhost", Port: 3100})

  handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("Replay server reached: %s => %s\n", r.RemoteAddr, r.URL.String())
    w.Write([]byte("replay server"))
  })

  srv1 := &http.Server{Addr: "localhost:3123"}
  srv2 := &http.Server{Addr: "localhost:3124"}
  srv1.Handler = handler
  srv2.Handler = handler

  go srv1.ListenAndServe()
  go srv2.ListenAndServe()

  replayer := replay.New("http://localhost:3123", "http://localhost:3124")
  replayer.SetHandler(func(err error, res *http.Response, req *http.Request) {
    if err != nil {
      fmt.Printf("Replay error: %s => %s\n", req.URL.String(), err)
      return
    }
    fmt.Printf("Replay response: %s => %d\n", req.URL.String(), res.StatusCode)
  })

  vs.Use(replayer)
  vs.Forward("http://httpbin.org")

  fmt.Printf("Server listening on port: %d\n", 3100)
  err := vs.Listen()
  if err != nil {
    fmt.Printf("Error: %s\n", err)
  }
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureTransporterFinalized

func EnsureTransporterFinalized(httpTransport *http.Transport)

EnsureTransporterFinalized will ensure that when the HTTP client is GCed the runtime will close the idle connections (so that they won't leak) this function was adopted from Hashicorp's go-cleanhttp package.

func NewReplayRequest

func NewReplayRequest(req *http.Request, host string, body io.Reader) (*http.Request, error)

NewReplayRequest creates a new http.Request cloning on the given one replacing the target URL host.

Types

type Filter

type Filter func(*http.Request) bool

Filter function is used to determine if a given http.Request should be replayed or not.

type Handler

type Handler func(error, *http.Response, *http.Request)

Handler function is used to

type Modifier

type Modifier func(*http.Request)

Modifier function is used to modify a given http.Request before replaying it.

type Replayer

type Replayer struct {
	// Targets stores the replay URL targets.
	Targets []string

	// Filters stores the replay filter functions.
	Filters []Filter

	// Modifiers stores the replay request modifier functions.
	Modifiers []Modifier

	// Handler stores the optional replay response/error handler.
	Handler Handler

	// Client stores the http.Client to be used to replay the requests.
	// Defaults to http.DefaultClient.
	Client *http.Client
}

Replayer replays incoming HTTP traffic to one or multiple servers via isolated goroutine.

func New

func New(targets ...string) *Replayer

New creates a new replayer ready to be attached as middleware.

func (*Replayer) Filter

func (x *Replayer) Filter(fn ...Filter) *Replayer

Filter attaches a new filter function to the current replayer who determines if a given request should be replayed or not.

func (*Replayer) HandleHTTP

func (x *Replayer) HandleHTTP(w http.ResponseWriter, r *http.Request, h http.Handler)

HandleHTTP handles an incoming HTTP request received by the proxy.

func (*Replayer) Modify

func (x *Replayer) Modify(fn ...Modifier) *Replayer

Modify attaches a new modifier function to the current replayer who is responsible to modify the http.Request to be replayed before the replay.

func (*Replayer) Replay

func (x *Replayer) Replay(r *http.Request, target string, body io.Reader)

Replay replays the given http.Request to the given target hostname.

func (*Replayer) SetHandler

func (x *Replayer) SetHandler(fn Handler) *Replayer

SetHandler is used to set a replay request handler, allowing the developer to deal with the replay response or error accordingly.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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