httpware

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2019 License: MIT Imports: 1 Imported by: 0

README

Build Status Maintainability Test Coverage Go Report Card GoDoc

httpware

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware represents an http server middleware it wraps an http.Handler with another one

func (Middleware) Append added in v1.0.0

func (m Middleware) Append(middlewares ...Middleware) Middlewares

Append will add given middlewares after existing one t1.Append(t2, t3) => [t1, t2, t3] t1.Append(t2, t3).DecorateHandler(<yourHandler>) == t1(t2(t3(<yourHandler>)))

func (Middleware) Prepend added in v1.0.0

func (m Middleware) Prepend(middlewares ...Middleware) Middlewares

Prepend will add given middlewares before existing one t1.Prepend(t2, t3) => [t2, t3, t1] t1.Prepend(t2, t3).DecorateHandler(<yourHandler>) == t2(t3(t1(<yourHandler>)))

type Middlewares

type Middlewares []Middleware

[t1, t2, t3].DecorateHandler(<yourHandler>) == t1(t2(t3(<yourHandler>)))

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(
		addCustomResponseHeader,
		logResponseHeaders,
	)
	// 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) Append added in v1.0.0

func (m Middlewares) Append(middleware ...Middleware) Middlewares

Append will add given middleware after existing one [t1, t2].Append(t3, t4) => [t1, t2, t3, t4] [t1, t2].Append(t3, t4).DecorateHandler(<yourHandler>) == t1(t2(t3(t4(<yourHandler>))))

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()

func (Middlewares) Prepend added in v1.0.0

func (m Middlewares) Prepend(middleware ...Middleware) Middlewares

Prepend will add given middleware before existing one [t1, t2].Prepend(t3, t4) => [t3, t4, t1, t2] [t1, t2].Prepend(t3, t4).DecorateHandler(<yourHandler>) == t3(t4(t1(t2(<yourHandler>))))

type RoundTripFunc added in v0.2.0

type RoundTripFunc func(*http.Request) (*http.Response, error)

RoundTripFunc wraps a func to make it into an http.RoundTripper. Similar to http.HandleFunc.

func (RoundTripFunc) RoundTrip added in v0.2.0

func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements RoundTripper interface

type Tripperware

type Tripperware func(http.RoundTripper) http.RoundTripper

Tripperware represents an http client-side middleware (roundTripper middleware).

func (Tripperware) Append added in v1.0.0

func (t Tripperware) Append(tripperwares ...Tripperware) Tripperwares

Append will add given tripperwares after existing one t1.Append(t2, t3) == [t1, t2, t3] t1.Append(t2, t3).DecorateRoundTripper(<yourTripper>) == t1(t2(t3(<yourTripper>)))

func (Tripperware) DecorateClient added in v0.2.0

func (t Tripperware) DecorateClient(client *http.Client, clone bool) *http.Client

DecorateClient will decorate a given http.Client with the tripperware will return a clone of client if clone arg is true

func (Tripperware) Prepend added in v1.0.0

func (t Tripperware) Prepend(tripperwares ...Tripperware) Tripperwares

Prepend will add given tripperwares before existing one t1.Prepend(t2, t3) => [t2, t3, t1] t1.Prepend(t2, t3).DecorateRoundTripper(<yourTripper>) == t2(t3(t1(<yourTripper>)))

func (Tripperware) RoundTrip added in v0.3.0

func (t Tripperware) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements RoundTripper interface

type Tripperwares

type Tripperwares []Tripperware

[t1, t2, t3].DecorateRoundTripper(roundTripper) == t1(t2(t3(roundTripper)))

func TripperwareStack

func TripperwareStack(tripperwares ...Tripperware) Tripperwares

TripperwareStack allows to stack multi tripperware in order to decorate an http roundTripper

func (Tripperwares) Append added in v1.0.0

func (t Tripperwares) Append(tripperwares ...Tripperware) Tripperwares

Append will add given tripperwares after existing one [t1, t2].Append(t3, t4) == [t1, t2, t3, t4] [t1, t2].Append(t3, t4).DecorateRoundTripper(<yourTripper>) == t1(t2(t3(t4(<yourTripper>))))

func (Tripperwares) DecorateClient added in v0.2.0

func (t Tripperwares) DecorateClient(client *http.Client, clone bool) *http.Client

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

func (Tripperwares) Prepend added in v1.0.0

func (t Tripperwares) Prepend(tripperwares ...Tripperware) Tripperwares

Prepend will add given tripperwares before existing one [t1, t2].Prepend(t3, t4) == [t3, t4, t1, t2] [t1, t2].Prepend(t3, t4).DecorateRoundTripper(<yourTripper>) == t3(t4(t1(t2(<yourTripper>))))

func (Tripperwares) RoundTrip

func (t Tripperwares) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements RoundTripper interface it will decorate the http-client request and use the default `http.DefaultTransport` RoundTripper use `TripperwareStack(<yourTripperwares>).Decorate(<yourTripper>)` if you don't want to use `http.DefaultTransport`

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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