middlewares

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2018 License: MIT Imports: 10 Imported by: 0

README

GoDoc Build Status

Install

go get github.com/hawry/middlewares

Usage

Checkout the GoDoc page for the documentation: https://godoc.org/github.com/Hawry/middlewares

Testing

To see the test coverage and which lines that are being tested, run: go test -coverprofile=cp.out && go tool cover -html=cp.out

Documentation

Overview

Package middlewares aims to create a set of commonly used middleware http.Handlers for use with the default http package. All handlers only takes a http.Handler as an argument, and returns only http.Handler, to more easily be chained with handler chain libraries (e.g. https://github.com/justinas/alice)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowCORSExposedHeaders

func AllowCORSExposedHeaders(headers ...string)

AllowCORSExposedHeaders whitelists which headers are allowed for the browsers to access

func AllowCORSHeaders

func AllowCORSHeaders(headers ...string)

AllowCORSHeaders specifies which headers that can be used

func AllowCORSMethods

func AllowCORSMethods(methods ...string)

AllowCORSMethods specified which methods that are allowed for CORS

func AllowCORSOrigins

func AllowCORSOrigins(origins ...string)

AllowCORSOrigins specifies which origins to allow

func BasicAuthorizationHandler

func BasicAuthorizationHandler(next http.Handler) http.Handler

BasicAuthorizationHandler extracts any Authorization info of type Basic.

Example
defaultHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	// do something
})

http.Handle("/", BasicAuthorizationHandler(defaultHandler))
http.ListenAndServe(":3000", nil)

func BasicCredentials

func BasicCredentials(ctx context.Context) (user, pass string, err error)

BasicCredentials returns the user, pass or an error if the values could not be handled or doesn't exist in the context

Example
defaultHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	user, pass, err := BasicCredentials(r.Context())
	if err != nil {
		// error handling
	}
	fmt.Printf("%s, %s", user, pass)
})
// ...
http.Handle("/", defaultHandler)

func CORSHandler

func CORSHandler(next http.Handler) http.Handler

CORSHandler appends CORS headers to the response if any CORS headers are present in the request or as a preflight request. For detailed documentation regarding CORS and what headers mean, please see https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

Example
AllowCORSOrigins("http://example.com")  //requests from http://example.com are allowed
AllowCORSMethods("GET", "POST", "HEAD") //requests with method GET, POST and HEAD are allowed
AllowCORSHeaders("X-Requested-With")    //the server will be able to handle the header X-Requested-With

corsHandler := CORSHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	// ... do something
}))

http.Handle("/", corsHandler)
http.ListenAndServe(":8080", nil)

func ErrorHandler added in v0.1.4

func ErrorHandler(next http.Handler) http.Handler

ErrorHandler will inject a html response to any error status code (400/500 range)

func LoggingHandler

func LoggingHandler(next http.Handler) http.Handler

LoggingHandler returns a http.Handler that wraps next, and prints requests and responses in Apache Combined Log Format.

Example
defaultHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	// do something
})

http.Handle("/", LoggingHandler(defaultHandler))
http.ListenAndServe(":3000", nil)

func SetOutput

func SetOutput(w io.Writer)

SetOutput sets which io.Writer to print the log to. Default is os.Stdout.

Setting an output will change the default output for ALL handlers in this package.

Example
//This will print the log to logFile
logFile, _ := os.Open("logfile")
SetOutput(logFile)
Example (MultiWriter)
//This will print the log both to Stdout and a file. Any ínterface implementing io.Writer can be used.
logFile, _ := os.Open("logfile")
mw := io.MultiWriter(os.Stdout, logFile)
SetOutput(mw)

func SupportCredentials

func SupportCredentials(b bool)

SupportCredentials sets the Support-Credentials header to b

func Token

func Token(ctx context.Context) (token string, err error)

Token returns any Bearer tokens that was found using TokenHandler, if no token is found it returns an error

Example
defaultHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	token, err := Token(r.Context())
	if err != nil {
		// error handling
	}
	fmt.Printf("%s", token)
})
// ...
http.Handle("/", defaultHandler)

func TokenHandler

func TokenHandler(next http.Handler) http.Handler

TokenHandler extracts any Bearer tokens from the request

Example
defaultHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	// do something
})

http.Handle("/", TokenHandler(defaultHandler))
http.ListenAndServe(":3000", nil)

Types

This section is empty.

Jump to

Keyboard shortcuts

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