tollbooth_fasthttp

package
v0.0.0-...-0587adc Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2017 License: MIT Imports: 6 Imported by: 0

README

tollbooth_fasthttp

Fasthttp middleware for rate limiting HTTP requests.

Five Minutes Tutorial

package main

import (
	"time"

	"github.com/didip/tollbooth"
	"github.com/magicwrighter/tollbooth/thirdparty/tollbooth_fasthttp"
	"github.com/valyala/fasthttp"
)

func main() {
	requestHandler := func(ctx *fasthttp.RequestCtx) {
		switch string(ctx.Path()) {
		case "/hello":
			helloHandler(ctx)
		default:
			ctx.Error("Unsupporterd path", fasthttp.StatusNotFound)
		}
	}

	// Create a limiter struct.
	limiter := tollbooth.NewLimiter(1, time.Second)

	fasthttp.ListenAndServe(":4444", tollbooth_fasthttp.LimitHandler(requestHandler, limiter))
}

func helloHandler(ctx *fasthttp.RequestCtx) {
	ctx.SetStatusCode(fasthttp.StatusOK)
	ctx.SetBody([]byte("Hello, World!"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildKeys

func BuildKeys(limiter *config.Limiter, ctx *fasthttp.RequestCtx) [][]string

BuildKeys generates a slice of keys to rate-limit by given config and request structs.

func LimitByRequest

func LimitByRequest(limiter *config.Limiter, ctx *fasthttp.RequestCtx) *errors.HTTPError

func LimitHandler

func LimitHandler(handler fasthttp.RequestHandler, limiter *config.Limiter) fasthttp.RequestHandler

func RemoteIP

func RemoteIP(ipLookups []string, ctx *fasthttp.RequestCtx) string

RemoteIP finds IP Address given http.Request struct.

func StringInSlice

func StringInSlice(sliceString []string, needle string) bool

StringInSlice finds needle in a slice of strings.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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