middleware

package
Version: v2.31.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2021 License: MIT Imports: 24 Imported by: 0

README

Package Middleware

This package contains http middleware. The middleware are actually a pure http.Handler. We keep the handler in this way since it's the form that compatible with golang standard.

JWT Middleware

JWT middleware is middleware for checking whether token is valid or not.

Log Middleware

Log middleware is middleware that will help logging the application. The logging prints out log from Kitabisa log specification.

How To Use The Middleware

func main() {
	meta := structs.Meta{
		Version: "v1.2.3",
		Status:  "stable",
		APIEnv:  "prod-test",
	}

	handlerCtx := phttp.NewContextHandler(meta)
	
	newHandler := phttp.NewHttpHandler(handlerCtx)
	helloHandler := newHandler(HelloHandler)

    // middleware initialization. Just initialized it and use.
	midd := middleware.NewJWTMiddleware(handlerCtx, []byte("abcde"))
	logMiddleware := middleware.NewHttpRequestLoggerMiddleware(logger)

	router := chi.NewRouter()
    
    // use the middleware
    router.Use(midd)
    router.Use(logMiddleware)
    router.Use(RequestLogger) // will print request log only for 4xx and 5xx HTTP status code
    
	router.Get("/", helloHandler.ServeHTTP)

	http.ListenAndServe(":5678", router)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapHeaderToContext added in v2.30.0

func MapHeaderToContext(next http.Handler) (wrapped http.Handler)

MapHeaderToContext is used for assigning header to the request context to be processed in graphql resolver.

func NewAuthentication added in v2.28.0

func NewAuthentication(hctx phttp.HttpHandlerContext, authOption AuthOption) func(next http.Handler) http.Handler

Middleware authentication supports jwt or basic auth

func NewBasicAuth added in v2.28.0

func NewBasicAuth(hctx phttp.HttpHandlerContext, definedUsername, definedPassword string) func(next http.Handler) http.Handler

func NewHeaderCheck added in v2.7.0

func NewHeaderCheck(hctx phttp.HttpHandlerContext, secretKey string) func(next http.Handler) http.Handler

func NewHttpRequestLogger added in v2.7.0

func NewHttpRequestLogger(logger *log.Logger) func(next http.Handler) http.Handler

TODO: to be deprecated

func NewJWT added in v2.7.0

func NewJWT(hctx phttp.HttpHandlerContext, signKey []byte) func(next http.Handler) http.Handler

func NewPaseto added in v2.29.0

func NewPaseto(hctx phttp.HttpHandlerContext, publicKey string) func(next http.Handler) http.Handler

func RequestIDToContextAndLogMiddleware added in v2.15.0

func RequestIDToContextAndLogMiddleware(next http.Handler) http.Handler

RequestIDToContextAndLogMiddleware set X-Ktbs-Request-ID header value and logger to context

func RequestLogger added in v2.14.0

func RequestLogger(next http.Handler) http.Handler

RequestLogger middleware for request logging using zerolog

func WatermillGetProcessID added in v2.25.0

func WatermillGetProcessID(message *message.Message) string

func WatermillLoggerMiddleware added in v2.25.0

func WatermillLoggerMiddleware(h message.HandlerFunc) message.HandlerFunc

func WatermillProcessIDMiddleware added in v2.25.0

func WatermillProcessIDMiddleware(h message.HandlerFunc) message.HandlerFunc

func WatermillSetProcessID added in v2.25.0

func WatermillSetProcessID(message *message.Message)

Types

type AuthOption added in v2.28.0

type AuthOption struct {
	SignKey  []byte // jwt sign key
	Username string // Basic Auth username
	Password string // Basuc Auth password
}
type Header struct {
	XKtbsRequestID     string `valid:"uuidv4,required"`
	XKtbsAPIVersion    string `valid:"semver,required"`
	XKtbsClientVersion string `valid:"semver,required"`
	XKtbsPlatformName  string `valid:"required"`
	XKtbsClientName    string `valid:"required"`

	// Optional
	XKtbsSignature string `valid:"optional"`
	XKtbsTime      string `valid:"int,optional"`
	Authorization  string `valid:"optional"`
}

type HttpRequestLoggerMiddleware

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL