middlewares

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2024 License: MIT Imports: 21 Imported by: 0

README

Middlewares

Commonly used middlewares in microservices

Correlation

Try to access the Ctx_correlation_id in the header. If it's not exists it will create a new one and attach it to context.

package main

import (
	"github.com/go-chi/chi/v5"
	"github.com/infiniteloopcloud/middlewares"
)

func main() {
	r := chi.NewRouter()
	r.Use(middlewares.Correlation())
}
Context

Context middleware load the values sent in the header from the sender's context into the request's context.

package main

import (
	"github.com/go-chi/chi/v5"
	"github.com/infiniteloopcloud/middlewares"
)

func main() {
	r := chi.NewRouter()
	r.Use(middlewares.Context())
}
CORS

Setup CORS's server-side settings

package main

import (
	"github.com/go-chi/chi/v5"
    "github.com/go-chi/cors"
	"github.com/infiniteloopcloud/middlewares"
)

func main() {
	r := chi.NewRouter()
	r.Use(cors.Handler(middlewares.Cors()))
}
Log

Add custom request/response logging

package main

import (
	"github.com/go-chi/chi/v5"
	"github.com/infiniteloopcloud/log"
	"github.com/infiniteloopcloud/middlewares"
)

func main() {
	r := chi.NewRouter()
	r.Use(middlewares.CustomLog(middlewares.LogOpts{LogLevel: log.LevelToUint()}))
}
Prometheus

Collect metrics of request/responses

package main

import (
	"github.com/go-chi/chi/v5"
	"github.com/infiniteloopcloud/middlewares"
)

func main() {
	r := chi.NewRouter()
	r.Use(middlewares.Prometheus)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IsTTY bool

Functions

func ClientHost

func ClientHost() func(next http.Handler) http.Handler

func Context

func Context(contextKeys map[string]log.ContextField) func(next http.Handler) http.Handler

func Correlation

func Correlation() func(next http.Handler) http.Handler

func Cors

func Cors(opts ...CorsOpts) cors.Options

func CustomLog

func CustomLog(opts ...LogOpts) func(next http.Handler) http.Handler

CustomLog middleware is responsible for setting the logLevel

func CustomWriter added in v0.6.0

func CustomWriter() func(next http.Handler) http.Handler

CustomWriter wraps the http.ResponseWriter into a new hyper.Writer

func HTTPPath

func HTTPPath() func(next http.Handler) http.Handler

func LogResponse added in v0.6.0

func LogResponse() func(next http.Handler) http.Handler

LogResponse middleware is responsible for log the response body

func PrintPrettyStack added in v0.5.1

func PrintPrettyStack(ctx context.Context, rvr interface{})

func Prometheus

func Prometheus(next http.Handler) http.Handler

func Recoverer added in v0.5.1

func Recoverer(next http.Handler) http.Handler

Recoverer is a middleware that recovers from panics, logs the panic (and a backtrace), and returns a HTTP 500 (Internal Server Error) status if possible. Recoverer prints a request ID if one is provided.

Alternatively, look at https://github.com/go-chi/httplog middleware pkgs.

func SetRecoverParser added in v0.5.1

func SetRecoverParser(p RecoverParserIface)

func SetRecovererErrorWriter added in v0.5.3

func SetRecovererErrorWriter(w io.Writer)

func Tracing

func Tracing() func(next http.Handler) http.Handler

func XSSBodyValidation added in v0.5.0

func XSSBodyValidation() func(next http.Handler) http.Handler

Types

type CorsOpts

type CorsOpts struct {
	AllowedOrigins string
}

type LogOpts

type LogOpts struct {
	LogLevel uint8
}

type RecoverParserIface added in v0.5.1

type RecoverParserIface interface {
	Parse(ctx context.Context, debugStack []byte, rvr interface{}) ([]byte, error)
}
var RecoverParser RecoverParserIface = prettyStack{}

type RecovererLogStack added in v0.5.3

type RecovererLogStack struct {
	DebugStackLengthLimit int
}

func (RecovererLogStack) Parse added in v0.5.3

func (l RecovererLogStack) Parse(ctx context.Context, debugStack []byte, rvr interface{}) ([]byte, error)

Jump to

Keyboard shortcuts

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