Skip to content

didip/tollbooth_fasthttp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

tollbooth_fasthttp

Fasthttp middleware for rate limiting HTTP requests.

NOTE:

This shim uses v1.0.0 API.

Five Minutes Tutorial

package main

import (
	"time"

	"github.com/didip/tollbooth"
	"github.com/didip/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!"))
}

About

Tollbooth - Fasthttp integration layer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages