handlers

package
v1.12.2 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogHandler

func LogHandler(logger *logrus.Entry) func(handler http.Handler) http.Handler
Example
logrus.SetLevel(logrus.DebugLevel)

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(logrus.WithField("service", ""))(handle).(*loggerHandler)

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

Types

type LoggerResponseWriter

type LoggerResponseWriter struct {
	http.ResponseWriter
	StatusCode int
	ErrMsg     []byte
}

func (*LoggerResponseWriter) Write

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

func (*LoggerResponseWriter) WriteHeader

func (w *LoggerResponseWriter) WriteHeader(statusCode int)

Jump to

Keyboard shortcuts

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