gohttpmw

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: May 24, 2020 License: Apache-2.0 Imports: 7 Imported by: 1

README

Useful Go HTTP middlewares

Documentation Go Report Card Coverage Status CircleCI Maintainability

Content type

Simply set the content-type in the header.

RBAC

depends on

  • github.com/ory/ladon

Allows to check if a user is allowed to access the URL, according to his role. You need to specify the function that will get the role name from the context.

Request ID

depends on

  • github.com/rs/xid

Set a request id in the context as well as the header.

Security

Set the basic headers necessary for a better security. These are not sufficient, you will need to add more according to the context of your application.

Logger

If you want performance, use LoggerZero. Otherwise, simply use logrus. Add logs to the request If you use other middlewares, make sure they don't change the reference to the request

Documentation

Overview

Package gohttpmw contains various middlewares that are helpful when building your http application

Index

Constants

View Source
const (
	// ContextKeyAddToRequestLog allow storage of additional log fields in the context
	ContextKeyAddToRequestLog = ContextKey("AddToLog")
)
View Source
const ContextKeyRequestError = ContextKey("requestError")

ContextKeyRequestError will allow the error to be passed down

View Source
const (
	// ContextKeyRequestID allow storage of requestid in the context
	ContextKeyRequestID = ContextKey("requestID")
)

Variables

This section is empty.

Functions

func AddToRequestLog

func AddToRequestLog(
	k string,
	f func(context.Context) interface{},
) func(http.Handler) http.Handler

AddToRequestLog allows to add more fields to the request log

func CSP

func CSP(c string) func(http.Handler) http.Handler

CSP adds a CSP header to the response writer

func GetAddToRequestLog

func GetAddToRequestLog(ctx context.Context) map[string]interface{}

GetAddToRequestLog will retrieve the fileds to be added to the log

func GetRequestError

func GetRequestError(ctx context.Context) error

GetRequestError will retrieve the request error from the context if there is one

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID will retrieve the request id from the context if there is one

func Logger

func Logger(l *logrus.Logger) func(http.Handler) http.Handler

Logger will run the full request details if you need performance, look into loggerZero

func LoggerZero

func LoggerZero(logger zerolog.Logger) func(http.Handler) http.Handler

LoggerZero will log the full request details, with performance

func RBAC

func RBAC(
	warden ladon.Warden,
	getRoleFunc func(context.Context) string,
) func(http.Handler) http.Handler

RBAC checks if the user is allowed to do the request

func RequestID

func RequestID() func(http.Handler) http.Handler

RequestID adds a requestID to the request context ksuid is a unique global id that is orderable by time (a step up normal uuid)

func Security

func Security() func(http.Handler) http.Handler

Security adds secure headers

func SetRequestError

func SetRequestError(r *http.Request, err error)

SetRequestError sets the error in the context so it can be picked up for logging

Types

type ContextKey

type ContextKey string

ContextKey represents the key for context value

func (ContextKey) String

func (c ContextKey) String() string

Jump to

Keyboard shortcuts

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