limiterphi

package module
v0.0.0-...-24b206c Latest Latest
Warning

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

Go to latest
Published: May 20, 2022 License: MIT Imports: 4 Imported by: 0

README

Limiter Phi

CircleCI GitHub license codecov Go Report Card
phi middleware of ulule/limiter package.
For detailed documentation https://github.com/ulule/limiter

Example

package main

import (
    "github.com/akdilsiz/limiterphi"
    "github.com/fate-lovely/phi"
    "github.com/ulule/limiter/v3"
    "github.com/ulule/limiter/v3/drivers/store/memory"
    "github.com/valyala/fasthttp"
    "log"
)

func main() {
    store := memory.NewStore()

    rate, err := limiter.NewRateFromFormatted("10-M")
    if err != nil {
        panic(err)
    } 
    middleware := limiterphi.NewMiddleware(limiter.New(store, rate))

    router := phi.NewRouter()
    router.Use(middleware.Handle)
    router.Get("/", func(ctx *fasthttp.RequestCtx) {
        ctx.SetStatusCode(fasthttp.StatusOK)
        ctx.SetContentType("application/json")
        ctx.SetBodyString(`{"message":"OK"}`)
    })

    log.Fatal(fasthttp.ListenAndServe(":3001", router.ServeFastHTTP))
}

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultErrorHandler

func DefaultErrorHandler(ctx *fasthttp.RequestCtx, err error)

DefaultErrorHandler is the default ErrorHandler used by a new Middleware.

func DefaultKeyGetter

func DefaultKeyGetter(ctx *fasthttp.RequestCtx) string

DefaultKeyGetter is the default KeyGetter used by a new Middleware It returns the Client IP address

func DefaultLimitReachedHandler

func DefaultLimitReachedHandler(ctx *fasthttp.RequestCtx)

DefaultLimitReachedHandler is the default LimitReachedHandler used by a new Middleware.

Types

type ErrorHandler

type ErrorHandler func(ctx *fasthttp.RequestCtx, err error)

ErrorHandler is an handler used to inform when an error has occurred.

type KeyGetter

type KeyGetter func(ctx *fasthttp.RequestCtx) string

KeyGetter will define the rate limiter key given the phi Context

type LimitReachedHandler

type LimitReachedHandler func(ctx *fasthttp.RequestCtx)

LimitReachedHandler is an handler used to inform when the limit has exceeded.

type Middleware

type Middleware struct {
	Limiter        *limiter.Limiter
	OnError        ErrorHandler
	OnLimitReached LimitReachedHandler
	KeyGetter      KeyGetter
}

Middleware is the middleware for phi.

func NewMiddleware

func NewMiddleware(limiter *limiter.Limiter, options ...Option) *Middleware

NewMiddleware return a new instance of a phi middleware.

func (*Middleware) Handle

func (middleware *Middleware) Handle(next phi.HandlerFunc) phi.HandlerFunc

Handle phi request.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is used to define Middleware configuration

func WithErrorHandler

func WithErrorHandler(handler ErrorHandler) Option

WithErrorHandler will configure the Middleware to use the given ErrorHandler.

func WithKeyGetter

func WithKeyGetter(KeyGetter KeyGetter) Option

WithKeyGetter will configure the Middleware to use the given KeyGetter

func WithLimitReachedHandler

func WithLimitReachedHandler(handler LimitReachedHandler) Option

WithLimitReachedHandler will configure the Middleware to use the given LimitReachedHandler.

Jump to

Keyboard shortcuts

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