response

package
v0.0.0-...-e92691c Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2019 License: MIT Imports: 10 Imported by: 0

README

Responder!

Middleware package for responding to HTTP requests

Dependencies:

  • gorilla/context

Examples

import "github.com/random9s/cinder/middleware/response"

func success() http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        //Get responder from context
    	ctx := r.Context()
	    v := ctx.Value(response.Key)
	    resp, _ := v.(*response.Response)

	    //Write successful response
        var t = &struct{
            T bool `json:"T"`
        }{
            T:true,
        }

	    resp.WriteJSON(t)
    })
}

func err() http.Handler{
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        //Get responder from context
    	ctx := r.Context()
	    v := ctx.Value(response.Key)
	    resp, _ := v.(*response.Response)

        var err = errors.New("bad stuff")
	    resp.Error(err, http.StatusInternalServerError)
    })
}

func main() {
    var encodedWriter = response.Writer(response.GZIP)
    http.Handle("/success", encodedWriter(success()))
    http.Handle("/error", encodedWriter(err()))
    log.Fatal(http.ListenAndServe(":8080", nil))
}

Documentation

Index

Constants

View Source
const (
	GZIP   = "gzip"
	LZW    = "compress"
	FLATE  = "deflate"
	BROTLI = "br"
)

Response compressions

Variables

This section is empty.

Functions

func Writer

func Writer(contentEncoding string) func(http.Handler) http.Handler

Writer writes data to the responsewriter

Types

type Encoder

type Encoder func(*Response) ([]byte, error)

Encoder function compresses response

type Response

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

Response ...

func New

func New() *Response

New wraps responsewriter

func (*Response) Error

func (r *Response) Error(err error, status int) (int, error)

Error ...

func (*Response) Header

func (r *Response) Header() http.Header

Header returns http headers

func (*Response) Status

func (r *Response) Status() int

Status ...

func (*Response) WriteHeader

func (r *Response) WriteHeader(statusCode int)

WriteHeader ...

func (*Response) WriteJSON

func (r *Response) WriteJSON(v interface{}) (int, error)

WriteJSON returns a JSON Encoded server response

func (*Response) WriteXML

func (r *Response) WriteXML(v interface{}) (int, error)

WriteXML returns an XML Encdoded server response

func (*Response) Written

func (r *Response) Written() bool

Written checks if response has been written

Jump to

Keyboard shortcuts

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