handlers

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2019 License: MIT Imports: 12 Imported by: 0

README

Handlers

Documentation Go Report Card Last Release

A set of frequently used Go http handlers and middleware functions.

# download library
$: go get github.com/faryon93/handlers

# place on top of your go file
import "github.com/faryon93/handlers"

Content: Handlers

Handler Description
Forbidded() Default 403 forbidden handler.
NoRobots() Writes a robots.txt file, which disallows the access to everything.

Content: Adapters

Adapter functions can be chained with the real handler function or other adapter functions.

Adapter Description
CORS(age, orgins) Configures gorilla/handlers/cors in a convenient way
Benchmark() Logs the execution time of every request using logrus
Enabled(en) Denys access if en is false
Keyed(reqKey) Restrict access to requests, having param "key" matching reqKey
Paged(limit) Paging: Parses skip and limit from query parameters
Recaptcha(key) Restricts access if recaptcha is invalid
RestrictOrigin(orgins...) Rejects all request having an origin which is not on the list

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain

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

Chain chains a http.Handler with the given adapters.

func ChainFunc

func ChainFunc(h http.HandlerFunc, adapters ...Adapter) http.Handler

ChainFunc chains a http.HandlerFunc with the given adapater.

func Forbidden added in v1.0.1

func Forbidden(w http.ResponseWriter, r *http.Request)

Forbidden returns a "403 forbidded" error.

func GetPageLimit added in v1.0.2

func GetPageLimit(r *http.Request) int

GetPageLimit returns the number of elements per page aka limit of the Paged() Adapater. To use this funktion the Paged() adapter need to be chained in.

func GetPageSkip added in v1.0.2

func GetPageSkip(r *http.Request) int

GetPageSkip returns the element offset aka skip of the Paged() Adapater. To use this funktion the Paged() adapter need to be chained in.

func NoRobots

func NoRobots(w http.ResponseWriter, r *http.Request)

NoRobots writes a robots.txt file which disallows access to everything.

Types

type Adapter

type Adapter func(http.Handler) http.Handler

Adapter is a function which implements a middleware handler.

func Benchmark added in v1.0.2

func Benchmark() Adapter

Benchmark logs the exection time of each request using logrus. In order to work correctly this Adapter should be the first Adapter in the chain.

func CORS added in v1.0.2

func CORS(age int, origins ...string) Adapter

CORS creates a gorilla CORS adapter. The age parameter is in seconds.

func Enabled

func Enabled(enabled bool, opts ...interface{}) Adapter

Enabled denys access to fn() if enabled is false.

func Keyed

func Keyed(key string, opts ...interface{}) Adapter

Keyed restricts the execution of fn() to requests which have the query parameter "key" matched with the user supplied key.

func Paged added in v1.0.2

func Paged(defaultLimit string, opts ...interface{}) Adapter

Paged parses the limit and skip parameter and stores it to the requests context.

func Recaptcha added in v1.0.2

func Recaptcha(key string, opts ...interface{}) Adapter

Recaptcha verifies the given recaptcha response. If the verification failes the processing of the request is canceled. If the key parameter is empty, the captcha check is bypassed.

func RestrictOrigin added in v1.0.2

func RestrictOrigin(origins []string, opts ...interface{}) Adapter

RestrictOrigin prevents further processing if the request origin is not on the origins list.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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