Documentation ¶
Index ¶
- type Middleware
- type Middlewares
- type RoundTripFunc
- type Tripperware
- type Tripperwares
- func (t Tripperwares) DecorateClient(client *http.Client, clone bool) *http.Client
- func (t Tripperwares) DecorateRoundTripFunc(tripper RoundTripFunc) http.RoundTripper
- func (t Tripperwares) DecorateRoundTripper(tripper http.RoundTripper) http.RoundTripper
- func (t Tripperwares) RoundTrip(req *http.Request) (*http.Response, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Middleware ¶
Middleware represents an http server middleware it wraps an http.Handler with another one
type Middlewares ¶
type Middlewares []Middleware
func MiddlewareStack ¶
func MiddlewareStack(middlewares ...Middleware) Middlewares
MiddlewareStack allows you to stack multiple middleware collection in a specific order
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/gol4ng/httpware" ) func main() { // create a middleware that adds a requestId header on each http-server request addCustomResponseHeader := func(h http.Handler) http.Handler { return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) { writer.Header().Add("custom-response-header", "wonderful header value") h.ServeHTTP(writer, req) }) } // create a middleware that logs the response header on each call logResponseHeaders := func(h http.Handler) http.Handler { return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) { fmt.Println("http response headers : ", writer.Header()) h.ServeHTTP(writer, req) }) } // create the middleware stack stack := httpware.MiddlewareStack( logResponseHeaders, addCustomResponseHeader, ) // create a server srv := http.NewServeMux() // apply the middlewares on the server // note: this part is normally done on `http.ListenAndServe(":<serverPort>", stack.DecorateHandler(srv))` h := stack.DecorateHandler(srv) // fake a request req := httptest.NewRequest(http.MethodGet, "/", nil) rr := httptest.NewRecorder() h.ServeHTTP(rr, req) }
Output: http response headers : map[Custom-Response-Header:[wonderful header value]]
func (Middlewares) DecorateHandler ¶ added in v0.2.0
func (m Middlewares) DecorateHandler(handler http.Handler) http.Handler
DecorateHandler will decorate a given http.Handler with the given middlewares created by MiddlewareStack()
func (Middlewares) DecorateHandlerFunc ¶ added in v0.2.0
func (m Middlewares) DecorateHandlerFunc(handler http.HandlerFunc) http.Handler
DecorateHandler will decorate a given http.HandlerFunc with the given middleware collection created by MiddlewareStack()
type RoundTripFunc ¶ added in v0.2.0
RoundTripFunc wraps a func to make it into an http.RoundTripper. Similar to http.HandleFunc.
type Tripperware ¶
type Tripperware func(http.RoundTripper) http.RoundTripper
Tripperware represents an http client-side middleware (roundTripper middleware).
func (Tripperware) DecorateClient ¶ added in v0.2.0
DecorateClient will decorate a given http.Client with the tripperware will return a clone of client if clone arg is true
type Tripperwares ¶
type Tripperwares []Tripperware
func TripperwareStack ¶
func TripperwareStack(tripperwares ...Tripperware) Tripperwares
TripperwareStack allows to stack multi tripperware in order to decorate an http roundTripper
func (Tripperwares) DecorateClient ¶ added in v0.2.0
DecorateClient will decorate a given http.Client with the tripperware collection will return a clone of client if clone arg is true
func (Tripperwares) DecorateRoundTripFunc ¶ added in v0.2.0
func (t Tripperwares) DecorateRoundTripFunc(tripper RoundTripFunc) http.RoundTripper
DecorateRoundTripFunc will decorate a given RoundTripFunc with the tripperware collection
func (Tripperwares) DecorateRoundTripper ¶ added in v0.2.0
func (t Tripperwares) DecorateRoundTripper(tripper http.RoundTripper) http.RoundTripper
DecorateRoundTripper will decorate a given http.RoundTripper with the tripperware collection