Documentation ¶
Overview ¶
Package httpinfo is a http middleware that wraps and adds data ( as response status, response time, ...) and help the retrieval of these information.
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/krostar/httpinfo" ) func myMiddleware(next http.Handler) http.HandlerFunc { return func(rw http.ResponseWriter, r *http.Request) { next.ServeHTTP(rw, r) if httpinfo.IsUsed(r) { fmt.Printf("status = %d\n", httpinfo.Status(r)) fmt.Printf("bytes wrote = %d\n", httpinfo.ContentLength(r)) } } } func myHandler(rw http.ResponseWriter, _ *http.Request) { rw.Write([]byte("Hello world")) // nolint: errcheck, gosec rw.WriteHeader(http.StatusAlreadyReported) } func main() { var srv = httptest.NewServer( httpinfo.Record()( myMiddleware(http.HandlerFunc(myHandler)), ), ) defer srv.Close() resp, err := http.DefaultClient.Get(srv.URL) if err != nil { panic(err) } defer resp.Body.Close() // nolint: errcheck, gosec }
Output: status = 208 bytes wrote = 11
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContentLength ¶
ContentLength returns the response content length. The value is likely to change throughout the execution of a request (each time ResponseWriter.Write is called). It is advised to only use this function after calling the next handler.
func ExecutionTime ¶
ExecutionTime returns the duration since the request start. The value will only have sens after the next handler returned. It is advised to only use this function after calling the next handler.
func Record ¶
Record records the http response information and helps to reach them from any other middleware. See examples on how to use it.