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 ¶
- func AllowCORSExposedHeaders(headers ...string)
- func AllowCORSHeaders(headers ...string)
- func AllowCORSMethods(methods ...string)
- func AllowCORSOrigins(origins ...string)
- func BasicAuthorizationHandler(next http.Handler) http.Handler
- func BasicCredentials(ctx context.Context) (user, pass string, err error)
- func CORSHandler(next http.Handler) http.Handler
- func ErrorHandler(next http.Handler) http.Handler
- func LoggingHandler(next http.Handler) http.Handler
- func SetOutput(w io.Writer)
- func SupportCredentials(b bool)
- func Token(ctx context.Context) (token string, err error)
- func TokenHandler(next http.Handler) http.Handler
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 ¶
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 ¶
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 ¶
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
ErrorHandler will inject a html response to any error status code (400/500 range)
func LoggingHandler ¶
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 ¶
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 ¶
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 ¶
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.