middleware

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2023 License: MIT Imports: 6 Imported by: 0

README

Here are a few example middlewares for natsrouter. The middlewares are:

  • auth.go: Authentication middleware that authenticates the message with an authorization token using the header tag 'authorization' or a specified header tag.
  • requestid.go: Request ID middleware that parses the header for a request id (storing it into the request context), or generates a unique ID for each request using the header tag 'request_id' or a specified header tag.
  • metrics.go: Metrics middleware that collects Prometheus metrics for NATS messages including the total number of messages received, duration of requests, and size of payloads.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotAuthorized = errors.New("not authorized")
View Source
var ErrTokenMissing = errors.New("authorization token not found")

Functions

func MetricsMiddleware

func MetricsMiddleware(next natsrouter.NatsCtxHandler) natsrouter.NatsCtxHandler

Middleware that increments the message count metric

func RequestIdFromContext

func RequestIdFromContext(ctx context.Context) string

Get current request Id from the context

func RequestIdMiddleware

func RequestIdMiddleware(tags ...string) func(next natsrouter.NatsCtxHandler) natsrouter.NatsCtxHandler

An example request id middleware

func TokenFromContext

func TokenFromContext(ctx context.Context) string

Get auth token from the context

Types

type AuthCallback

type AuthCallback func(token string) bool

Callback that must return true if authorized, false otherwise

type AuthMiddleware

type AuthMiddleware struct {
	// contains filtered or unexported fields
}

func NewAuthMiddleware

func NewAuthMiddleware(cb AuthCallback, tags ...string) *AuthMiddleware

Create a new authentication middleware, with optional header tag(s) If header tag is not defined, it defaults to `authorization`

func (*AuthMiddleware) Auth

Authentication middleware function

Jump to

Keyboard shortcuts

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