Categorygithub.com/cognusion/go-prw
repositorypackage
1.3.0
Repository: https://github.com/cognusion/go-prw.git
Documentation: pkg.go.dev

# README

prw

import "github.com/cognusion/go-prw"

Overview

Package prw provides PluggableResponseWriter, which is a ResponseWriter and Hijacker (for websockets) that provides reusability and resiliency, optimized for handler chains where multiple middlewares may want to modify the response. It also can Marshal/Unmarshal the core response parts (body, status, headers) for use with caching operations.

Index

Package files

prw.go

type PluggableResponseWriter

type PluggableResponseWriter struct {
    Body *bytes.Buffer
    // contains filtered or unexported fields
}

PluggableResponseWriter is a ResponseWriter that provides reusability and resiliency, optimized for handler chains where multiple middlewares may want to modify the response

func NewPluggableResponseWriter

func NewPluggableResponseWriter() *PluggableResponseWriter

NewPluggableResponseWriter returns a pointer to an initialized PluggableResponseWriter

func NewPluggableResponseWriterFromOld

func NewPluggableResponseWriterFromOld(rw http.ResponseWriter) *PluggableResponseWriter

NewPluggableResponseWriterFromOld returns a pointer to an initialized PluggableResponseWriter, with the original stored away for Flush()

func NewPluggableResponseWriterIfNot

func NewPluggableResponseWriterIfNot(rw http.ResponseWriter) (*PluggableResponseWriter, bool)

NewPluggableResponseWriterIfNot returns a pointer to an initialized PluggableResponseWriter and true, if the provided ResponseWriter is not a PluggableResponseWriter, otherwise returns the provided ResponseWriter casted as a PluggableResponseWriter and false. This makes simple create-and-clean stanzas trivial.

Where "w" is the original ResponseWriter passed rw, firstRw := NewPluggableResponseWriterIfNot(w) defer rw.FlushToIf(w, firstRw)

func (*PluggableResponseWriter) AddFlushFunc

func (w *PluggableResponseWriter) AddFlushFunc(f func(http.ResponseWriter, *PluggableResponseWriter))

AddFlushFunc adds a function to run if any of the Flush methods are called, to customize that activity

func (*PluggableResponseWriter) Close

func (w *PluggableResponseWriter) Close()

Close should only be called if the PluggableResponseWriter will no longer be used.

func (*PluggableResponseWriter) Code

func (w *PluggableResponseWriter) Code() int

Code returns the HTTP status code

func (*PluggableResponseWriter) Flush

func (w *PluggableResponseWriter) Flush()

Flush satisfies http.Flusher. If NewPluggableResponseWriterFromOld or NewPluggableResponseWriterIfNot is used, then the first time Flush() is called, if the original ResponseWriter is an http.Flusher, all headers and the body thus far are written to it, and then Flush() is called on it too. ALSO further Write() calls are also written to the original. Subsequent calls to Flush will call Flush() on the original.

func (*PluggableResponseWriter) FlushTo

func (w *PluggableResponseWriter) FlushTo(to http.ResponseWriter) (int, error)

FlushTo writes to the provided ResponseWriter with our headers, status code, and body. The PluggableResponseWriter should not be used after calling FlushToIf.

func (*PluggableResponseWriter) FlushToIf

func (w *PluggableResponseWriter) FlushToIf(to http.ResponseWriter, first bool) (int, error)

FlushToIf takes a ResponseWriter and boolean, and calls FlushTo if the boolean is true. The PluggableResponseWriter should not be used after calling FlushToIf. This makes simple create-and-clean stanzas trivial.

Where "w" is the original ResponseWriter passed rw, firstRw := NewPluggableResponseWriterIfNot(w) defer rw.FlushToIf(w, firstRw)

func (*PluggableResponseWriter) Header

func (w *PluggableResponseWriter) Header() http.Header

Header returns the current http.Header

func (*PluggableResponseWriter) Hijack

func (w *PluggableResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack implements http.Hijacker

func (*PluggableResponseWriter) Length

func (w *PluggableResponseWriter) Length() int

Length returns the byte length of the response body

func (*PluggableResponseWriter) MarshalBinary

func (w *PluggableResponseWriter) MarshalBinary() ([]byte, error)

MarshalBinary is used by encoding/gob to create a representation for encoding.

func (*PluggableResponseWriter) SetHeader

func (w *PluggableResponseWriter) SetHeader(h http.Header)

SetHeader takes an http.Header to replace the current with

func (*PluggableResponseWriter) SetHeadersToAdd

func (w *PluggableResponseWriter) SetHeadersToAdd(headers map[string]string)

SetHeadersToAdd sets a map of headers to add before flushing/writing headers to the response

func (*PluggableResponseWriter) SetHeadersToRemove

func (w *PluggableResponseWriter) SetHeadersToRemove(headers []string)

SetHeadersToRemove sets a list of headers to remove before flushing/writing headers to the response

func (*PluggableResponseWriter) UnmarshalBinary

func (w *PluggableResponseWriter) UnmarshalBinary(data []byte) error

UnmarshalBinary is used by encoding/gob to reconstitute a previously-encoded instance.

func (*PluggableResponseWriter) Write

func (w *PluggableResponseWriter) Write(b []byte) (int, error)

Write writes the data to the connection as part of an HTTP reply. Additionally, it sets the status if that hasn't been set yet, and determines the Content-Type if that hasn't been determined yet.

func (*PluggableResponseWriter) WriteHeader

func (w *PluggableResponseWriter) WriteHeader(status int)

WriteHeader sends an HTTP response header with the provided status code.


Generated by godoc2md