handlers

package
v1.20.2 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogHandler

func LogHandler() func(handler http.Handler) http.Handler
Example
ctx := logr.WithLogger(context.Background(), logr.StdLogger())

var handle http.HandlerFunc = func(rw http.ResponseWriter, req *http.Request) {
	time.Sleep(20 * time.Millisecond)

	switch req.Method {
	case http.MethodGet:
		rw.WriteHeader(http.StatusOK)
		_, _ = rw.Write([]byte(`{"status":"ok"}`))
	case http.MethodPost:
		rw.WriteHeader(http.StatusNoContent)
	case http.MethodDelete:
		rw.WriteHeader(http.StatusBadRequest)
		_, _ = rw.Write([]byte(`{"key":"StatusBadRequest","msg":"something wrong"}`))
	case http.MethodPut:
		rw.WriteHeader(http.StatusInternalServerError)
		_, _ = rw.Write([]byte(`{"key":"StatusInternalServerError","msg":"internal server error"}`))
	}
}

handler := LogHandler()(handle).(*loggerHandler)

for _, method := range []string{http.MethodGet, http.MethodPut, http.MethodDelete, http.MethodPost} {
	req, _ := http.NewRequestWithContext(ctx, method, "/", nil)
	handler.ServeHTTP(testify.NewMockResponseWriter(), req)
}
Output:

Types

type LoggerResponseWriter

type LoggerResponseWriter struct {
	// contains filtered or unexported fields
}

func (*LoggerResponseWriter) Header

func (rw *LoggerResponseWriter) Header() http.Header

func (*LoggerResponseWriter) Write

func (rw *LoggerResponseWriter) Write(data []byte) (int, error)

func (*LoggerResponseWriter) WriteErr

func (rw *LoggerResponseWriter) WriteErr(err error)

func (*LoggerResponseWriter) WriteHeader

func (rw *LoggerResponseWriter) WriteHeader(statusCode int)

Jump to

Keyboard shortcuts

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