wrap

package module
v0.0.0-...-c867904 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2016 License: Apache-2.0 Imports: 7 Imported by: 0

README

Wrap middleware handlers

Chain Wrappers for http Handlers or middle ware chaining

  • Chain creates an ordered chain of handlers from an argument list

  • ChainLinkWrap wraps each handler in the argument list of handlers calls in a single env

  • As an example wrapping middleware can be done with a recover wrapper [R] can wrap the chain of handlers

    // The handlers call chain A->B->C => R(A->B->C)
    handler := R(Chain(A,B,C))
  • Or recover could wrap each of the individual handlers in the chain
    // The handlers call chain A->B->C => R(A)->R(B)->R(C)
    handler := ChainLinkWrap(R,A,B,C)
  • Or recover could wrap one handler
    // The handlers call chain A->B->C =>  A->B->R(C)
    handler := Chain(A,B,R(C))
  • Example buffered handlers using a buffer pool bytes.Buffer might be used like the following.

  • Simple buffer

    handler := HttpScopedHandlerWriter(ChainLinkWrap(R,A,B,C))
  • Buffer pool
    handler := HttpScopedBPHandlerWriter(ChainLinkWrap(R,A,B,C))

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BPAlloc = 16384
View Source
var BPSize = 32
View Source
var NoOp = http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
	defer tracer.Enable(enable).ScopedTrace()()
})

Functions

func BufferPool

func BufferPool() *bpool.SizedBufferPool

func Chain

func Chain(handlers ...http.HandlerFunc) http.Handler

Chain creates an ordered chain of handlers from an argument list The handlers call chain A->B->C => R(A)->R(B)->R(C)

func ChainLinkWrap

func ChainLinkWrap(wrapper ChainerFunc, handlers ...http.HandlerFunc) http.Handler

ChainLinkWrap wraps each handler in the argument list of handlers The handlers call chain A->B->C => R(A->B->C)

func EnableTrace

func EnableTrace(e bool) bool

func HttpScopedBPHandlerWriter

func HttpScopedBPHandlerWriter(handler http.Handler) http.Handler

use a buffer buffer pools buffer then write/flush the buffer to the ResponseWriter.

func HttpScopedBufferHandler

func HttpScopedBufferHandler(handler http.Handler) http.Handler

func HttpScopedHandlerWriter

func HttpScopedHandlerWriter(handler http.Handler) http.Handler

use a bytes.Buffer then write/flush the buffer to the ResponseWriter

func Recover

func Recover(next http.Handler) http.Handler

Recover recovers from any panicking goroutine

func RecoverFunc

func RecoverFunc(next http.HandlerFunc) http.HandlerFunc

func TraceEnvConfig

func TraceEnvConfig() bool

turn on call trace for debug and testing

func Tracer

func Tracer() *trace.Tracer

Types

type Buffer

type Buffer interface {
	Context(ctxPtr interface{}) bool
	SetContext(ctxPtr interface{})
	Header() http.Header
	WriteHeader(i int)
	Write(b []byte) (int, error)
	Reset()
	FlushAll()
	Body() []byte
	BodyString() string
	HasChanged() bool
	IsOk() bool
	FlushCode()
	FlushHeaders()
}

type BufferWriter

type BufferWriter struct {

	// ResponseWriter is the underlying response writer that is wrapped
	// by BufferWriter
	http.ResponseWriter

	// BufferWriter is the underlying io.Writer that buffers the response
	// body
	Buffer *bytes.Buffer

	// Code is the cached status code
	Code int
	// contains filtered or unexported fields
}

Buffer is a ResponseWriter wrapper that may be used as buffer.

func NewBufferPoolWriter

func NewBufferPoolWriter(w http.ResponseWriter) (bf *BufferWriter)

NewBufferPoolWriter returns a BufferWriter wrapping the given response writer.

func NewBufferWriter

func NewBufferWriter(w http.ResponseWriter) (bf *BufferWriter)

NewBufferWriter returns a BufferWriter wrapping the given response writer.

func (*BufferWriter) BPFlushAll

func (bf *BufferWriter) BPFlushAll()

FlushAll flushes headers, status code and body to the underlying ResponseWriter, if something changed

func (*BufferWriter) Body

func (bf *BufferWriter) Body() []byte

Body returns the bytes of the underlying buffer (that is meant to be the body of the response)

func (*BufferWriter) BodyString

func (bf *BufferWriter) BodyString() string

BodyString returns the string of the underlying buffer (that is meant to be the body of the response)

func (*BufferWriter) FlushAll

func (bf *BufferWriter) FlushAll()

FlushAll flushes headers, status code and body to the underlying ResponseWriter, if something changed

func (*BufferWriter) FlushCode

func (bf *BufferWriter) FlushCode()

FlushCode flushes the status code to the underlying responsewriter if it was set.

func (*BufferWriter) FlushHeaders

func (bf *BufferWriter) FlushHeaders()

FlushHeaders adds the headers to the underlying ResponseWriter, removing them from BufferWriter.

func (*BufferWriter) HasChanged

func (bf *BufferWriter) HasChanged() bool

HasChanged returns true if Header, WriteHeader or Write has been called

func (*BufferWriter) Header

func (bf *BufferWriter) Header() http.Header

Header returns the cached http.Header and tracks this call as change

func (*BufferWriter) IsOk

func (bf *BufferWriter) IsOk() bool

IsOk returns true if the cached status code is not set or in the 2xx range.

func (*BufferWriter) Reset

func (bf *BufferWriter) Reset()

Reset set the BufferWriter to the defaults

func (*BufferWriter) Write

func (bf *BufferWriter) Write(b []byte) (int, error)

Write writes to the underlying buffer and tracks this call as change

func (*BufferWriter) WriteHeader

func (bf *BufferWriter) WriteHeader(i int)

WriteHeader writes the cached status code and tracks this call as change

type ChainerFunc

type ChainerFunc func(http.Handler) http.Handler

type Chainer http.HandlerFunc

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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