middleware

package
v2.22.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2021 License: MIT Imports: 17 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 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 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

Types

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
y or Y : Canonical URL