adaptd

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2020 License: MIT Imports: 6 Imported by: 2

README

adaptd

Go Report Card GoDoc

Adapters to add middleware to HTTP Handlers.

Installing

Use go get:

go get github.com/dadamssolutions/adaptd

Or, in go.mod:

require (
    github.com/dadamssolutions/adaptd
)

Examples

import (
    "net/http"
    "github.com/dadamssolutions/adaptd"
)

func main() {
    // Index handler should enure that HTTPS is used
    http.Handle("/", adaptd.EnsureHTTPS(false)(indexHandler))

    // Login handler should use HTTPS and handle GET and POST requests
    // Use Adapt to add multiple Adapters at once.
    // Be sure to check HTTPS first.
    loginHandler = adaptd.Adapt(loginHandler,
                    adaptd.EnsureHTTPS(false),
                    adaptd.GetAndOtherRequest(loginPostHandler, http.MethodPost))
    http.Handle("/login", loginHandler)

    http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil)
}

Contributing

Submit a pull request.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

Documentation

Overview

Package adaptd provides a simple adapter interface for adding middleware to http frameworks

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Adapt

func Adapt(h http.Handler, adapters ...Adapter) http.Handler

Adapt is a helper to add all the adapters required for a given http.Handler. Adapters will be called in the order they are given when the returned http.Handler is called.

func HTTPSRedirect

func HTTPSRedirect(port string) http.Handler

HTTPSRedirect adapter redirects all HTTP requests to HTTPS requests. Most users should simply call this as go http.ListenAndServe(":80", HTTPSRedirect("443"))

Types

type Adapter

type Adapter func(http.Handler) http.Handler

Adapter is a type that helps with http middleware.

func AddCookieWithFunc

func AddCookieWithFunc(name string, tg func(http.ResponseWriter, *http.Request) error) Adapter

AddCookieWithFunc adds the header before calling the handler. This is useful for things like CSRF tokens.

func AddHeader

func AddHeader(name, value string) Adapter

AddHeader adapter adds the header before calling the handler

func AddHeaderWithFunc

func AddHeaderWithFunc(name string, tg func() string) Adapter

AddHeaderWithFunc adds the header before calling the handler. This is useful for things like CSRF tokens.

func CheckAndRedirect

func CheckAndRedirect(f HandlerChecker, redirect http.Handler, logOnRedirect string) Adapter

CheckAndRedirect adapter checks the return of the function. On false, it redirects to the given URL. On true, it will call the handler passed to the Adapater.

func CountHTTPResponses added in v1.0.2

func CountHTTPResponses() Adapter

CountHTTPResponses calls the handler and records the response as a prometheus counter with labels endpoint, code, and method. This should be applied once for an entire web server.

func DisallowLongerPaths

func DisallowLongerPaths(path string, notFoundHandler http.Handler) Adapter

DisallowLongerPaths adapter calls the notFoundHandler if the URL path is longer than the registered one. For example, paths that do not match any registered handler are sent to the handler for "/". Adding this Adapter could display at custom 404 page.

func EnsureHTTPS

func EnsureHTTPS(allowXForwardedProto bool) Adapter

EnsureHTTPS adapter redirects an HTTP request to an HTTPS request. Some hosts forward requests and use 'X-Forward-Proto == "https"' to indicate that he request was made with https protocol. If you would like to allow this as a valid check, then the parameter should be true.

func GetAndOtherRequest

func GetAndOtherRequest(other http.Handler, method string) Adapter

GetAndOtherRequest adapter uses two handlers to handle both get requests and another. All other requests are given a http.StatusMethodNotAllowed error. The other handler is provided to create the Adapter while the get handler should be provided to the Adapter e.g. GetAndOtherRequest(other, http.MethodPost)(getHandler)

func Notify

func Notify(logger *log.Logger) Adapter

Notify adapter logs when the request is beginning to be processed and when it is finished.

func OnCheck

func OnCheck(f HandlerChecker, falseHandler http.Handler, logOnFalse string) Adapter

OnCheck adapter checks the return of the function. On false, it calls the handler. On true, it will call the handler passed to the Adapter.

func RequestMethod

func RequestMethod(method string) Adapter

RequestMethod adapter allow allows the given request method. All other requests are given a http.StatusMethodNotAllowed error.

func TrackHTTPResponseTimes added in v1.0.2

func TrackHTTPResponseTimes() Adapter

TrackHTTPResponseTimes calls the handler and records the response time as a prometheus summary with labels endpoint, code, and method. This should be applied once for an entire web server.

type HandlerChecker

type HandlerChecker func(http.ResponseWriter, *http.Request) bool

HandlerChecker checks the *http.Request and allows an Adapter to use different Handlers based on its return. For example, a HandlerChecker can check if a user is logged in when the login page is visited. If no user is logged in, the login page is shown. If a user is already logged in, then the Adapter might redirect to another page.

Jump to

Keyboard shortcuts

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