Documentation ¶
Overview ¶
Package finalizer implements an HTTP Middleware with a callback that's executed at the end of the HTTP request.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
func Middleware(finalizer ServerFinalizerFunc, next http.Handler) http.Handler
Middleware calls the ServerFinalizerFunc at the end of an HTTP Request.
Example ¶
//finalizer is executed at the end of the HTTP request in Middleware. finalizer := func(ctx context.Context, code int, r *http.Request) { header, _ := Header(ctx) size, _ := ResponseSize(ctx) contentType := header.Get("Content-Type") fmt.Printf("status=%d, contentType=%s, responseSize=%d\n", code, contentType, size) } handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusTeapot) w.Header().Set("Content-Type", "message/teapot") w.Write([]byte("I brew Tea")) }) // wrap the handler with the finalizer middleware. wrappedHandler := Middleware(finalizer, handler) srv := httptest.NewServer(wrappedHandler) defer srv.Close() if _, err := http.Get(srv.URL); err != nil { panic(err) }
Output: status=418, contentType=message/teapot, responseSize=0
Types ¶
Click to show internal directories.
Click to hide internal directories.