middleware

package
v0.0.0-...-c42a961 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// AccessGranted successful authorization
	AccessGranted = AccessCheck{}
	// AccessUnauthorized user unauthorized
	AccessUnauthorized = AccessCheck{http.StatusUnauthorized}
	// AccessForbidden users doesn't have required missing role
	AccessForbidden = AccessCheck{http.StatusForbidden}
)

Functions

func ACL

func ACL(authorizer Authorizer) func(http.Handler) http.Handler

ACL grants access for custom rules

func AccessLog

func AccessLog(opts AccessLogOptions) func(next http.Handler) http.Handler

AccessLog records all requests processed by the server.

func Authentication

func Authentication(next http.Handler) http.Handler

Authentication looking for user in headers and inject user to context

func Authorized

func Authorized(next http.Handler) http.Handler

Authorized grants access for non-guest users

func ContextLogger

func ContextLogger(factory logger.Factory) func(http.Handler) http.Handler

ContextLogger initialize logger entry for request context

Required for all routers as first middleware! At that moment we don't see reason pass loggerFactory to the each middleware - therefore all middlewares uses logger from context, otherwise will be used global logger and information could be incomplete.

func Newrelic

func Newrelic(app newrelic.Application) func(http.Handler) http.Handler

Newrelic create new transaction and set it to request context.

func OnlyRoles

func OnlyRoles(roles ...string) func(http.Handler) http.Handler

OnlyRoles grants access for given roles

func Prometheus

func Prometheus() func(http.Handler) http.Handler

func Recoverer

func Recoverer(handler PanicHandler) func(http.Handler) http.Handler

Recoverer panic error handling

func RequestID

func RequestID(prefix string) func(http.Handler) http.Handler

RequestID middleware takes request id from headers, set it into the context and pass to response headers.

Types

type AccessCheck

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

AccessCheck result of access check

type AccessCheckerFn

type AccessCheckerFn func(*userclient.User, *http.Request) AccessCheck

AuthorizerFn functional implementation of AccessChecker

func (AccessCheckerFn) Authorize

func (fn AccessCheckerFn) Authorize(user *userclient.User, r *http.Request) AccessCheck

type AccessLogOptions

type AccessLogOptions struct {
	IgnoreRequestBody  bool
	IgnoreResponseBody bool
}

AccessLogOptions for request or response body

type Authorizer

type Authorizer interface {
	// Authorize request
	// FYI: don't need to parse request path.
	// Request already has information about matched route chi.RouteContext(r.Context())
	Authorize(user *userclient.User, r *http.Request) AccessCheck
}

AccessChecker checks that's user has rights for access

type PanicHandler

type PanicHandler interface {
	ServeHTTP(w http.ResponseWriter, req *http.Request, err PanicRecoveredError)
}

PanicHandler interface Triggered on application panic and passing as last argument (rvr) value from recover()

type PanicHandlerFunc

type PanicHandlerFunc func(http.ResponseWriter, *http.Request, PanicRecoveredError)

PanicHandlerFunc implements PanicHandler interface

func (PanicHandlerFunc) ServeHTTP

ServeHTTP implements PanicHandler interface

type PanicRecoveredError

type PanicRecoveredError struct {
	Rvr   interface{}
	Stack []byte
}

func (PanicRecoveredError) Error

func (err PanicRecoveredError) Error() string

Jump to

Keyboard shortcuts

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