middleware

package
v0.0.0-...-1f40b3c Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2018 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Adapt

func Adapt(h http.HandlerFunc, middlewares ...Middleware) http.HandlerFunc

Adapt is used to wrap a http handler with Middleware

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/fossapps/starter/middleware"
)

func main() {
	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("handler function")
	})
	newHandler := middleware.Adapt(handler, getTestAdapter())
	newHandler(httptest.NewRecorder(), nil)
}

func getTestAdapter() middleware.Middleware {
	return func(handler http.Handler) http.HandlerFunc {
		return func(w http.ResponseWriter, r *http.Request) {
			fmt.Println("before")
			defer fmt.Println("after")
			handler.ServeHTTP(w, r)
		}
	}
}
Output:

before
handler function
after

Types

type LimiterOptions

type LimiterOptions struct {
	Namespace     string
	RequestHelper RequestHelper
	Limit         int
	AddHeaders    bool
	Logger        Logger
	Limiter       RateLimiter
	Jwt           jwt.Manager
}

LimiterOptions dependencies of Limit middleware

type Logger

type Logger interface {
	Warn(args ...interface{})
}

Logger implementation needed for Limit middleware to log

type Middleware

type Middleware func(http.Handler) http.HandlerFunc

Middleware should be returned from a func to be used as handler (a.k.a middlewares)

func AuthMw

func AuthMw(jwtManager jwt.Manager) Middleware

AuthMw ensures request is authenticated

func DevMw

func DevMw(milliseconds int) Middleware

DevMw simple middleware to delay a request

func Limit

func Limit(options LimiterOptions) Middleware

Limit middleware ensures a handler isn't called more than N times in D duraton

func MustHavePermission

func MustHavePermission(permission string, manager jwt.Manager) Middleware

MustHavePermission is a middleware which ensures a request has permission before handler is invoked

func ResponseTime

func ResponseTime(logger ResponseTimeLogger) Middleware

ResponseTime middleware to log response time for a request

type RateLimiter

type RateLimiter interface {
	Hit(key string) (int64, error)
	Count(key string) (int64, error)
}

RateLimiter implementation needed to work

type RequestHelper

type RequestHelper interface {
	GetIPAddress(r *http.Request) string
}

RequestHelper see RequestHelper in server.go

type ResponseTimeLogger

type ResponseTimeLogger interface {
	Info(args ...interface{})
}

ResponseTimeLogger interface implementation needed for ResponseTime for logging

Jump to

Keyboard shortcuts

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