donewriter

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2021 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package donewriter provides a simple wrapper around an http.ResponseWriter to track when a response has been sent. To use it, call the WrapWriter middleware early in your middleware stack. Then in other middlewares or handlers, you can use the WriterIsDone method to check the status.

func main() {
    r := chi.NewRouter()

    r.Use(donewriter.WrapWriter)
    // and other middlewares

    r.Get("/", func(w http.ResponseWriter, r *http.Request) {
        if done, _ := donewriter.WriterIsDone(w); done {
            // Nothing to do, a response was already sent
            return
        }

        // Normal operation here...

    })
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WrapWriter

func WrapWriter(next http.Handler) http.Handler

WrapWriter is an http middleware which wraps the standard http.ResponseWriter with a DoneWriter. Subsequent middlewares or handlers should use the WriterIsDone method to check the status.

func WriterIsDone

func WriterIsDone(w http.ResponseWriter) (bool, error)

WriterIsDone returns true if a response has been written. An error is returned if the underlying writer is not a DoneWriter.

Types

type DoneWriter

type DoneWriter struct {
	http.ResponseWriter
	Done bool
}

DoneWriter is an http.ResponseWriter which tracks its write state.

func (*DoneWriter) Write

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

func (*DoneWriter) WriteHeader

func (w *DoneWriter) WriteHeader(status int)

WriteHeader wraps the underlying WriteHeader method.

Jump to

Keyboard shortcuts

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