gear

package module
v0.0.0-...-437898f Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

This package provides Sentinel middleware for Gear.

Users may register SentinelMiddleware to the Gear router, like.

import (
	sentinelPlugear "github.com/sentinel-group/sentinel-go-adapters/gear"
	"github.com/teambition/gear"
)

r := gear.NewRouter()

r.Use(sentinelPlugear.SentinelMiddleware())

The plugear extracts "HttpMethod:Router" as the resource name by default (e.g. GET:/foo/:id). Users may provide customized resource name extractor when creating new SentinelMiddleware (via options).

Fallback logic: the plugear will return "429 Too Many Requests" status code if current request is blocked by Sentinel rules. Users may also provide customized fallback logic via WithBlockFallback(handler) options.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Example

func Example()

func SentinelMiddleware

func SentinelMiddleware(opts ...Option) gear.Middleware

SentinelMiddleware returns new gear.Middleware Default resource name is {method}:{path}, such as "GET:/api/users/:id" Default block fallback is returning 429 code Define your own behavior by setting options

Types

type Option

type Option func(*options)

func WithBlockFallback

func WithBlockFallback(fn func(ctx *gear.Context) error) Option

WithBlockFallback sets the fallback handler when requests are blocked.

func WithResourceExtractor

func WithResourceExtractor(fn func(*gear.Context) string) Option

WithResourceExtractor sets the resource extractor of the web requests.

Jump to

Keyboard shortcuts

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