handlers

package
v1.5.5 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: Apache-2.0 Imports: 8 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 := log.WithLogger(context.Background(), log.Std())

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(mock.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(sc int)

Jump to

Keyboard shortcuts

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