middlewares

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultCorsConfig = CorsConfig{
	AllowedOrigins: []string{"*"},
	AllowedMethods: []string{
		http.MethodGet, http.MethodPost, http.MethodPut,
		http.MethodPatch, http.MethodDelete, http.MethodOptions,
	},
}

DefaultCorsConfig is the default CORS config.

View Source
var DefaultLoggerConfig = LoggerConfig{
	Out:              os.Stdout,
	Level:            slog.LevelInfo,
	SuccessLevel:     slog.LevelInfo,
	ClientErrorLevel: slog.LevelWarn,
	ServerErrorLevel: slog.LevelError,
	Type:             TypeJSON,
}

DefaultLoggerConfig is the default logger config.

View Source
var DefaultRecoveryConfig = RecoveryConfig{
	LogRecovers: true,
	Writer:      os.Stdout,
	OnRecovery: func(c *kid.Context, err any) {
		c.JSON(http.StatusInternalServerError, kid.Map{"message": http.StatusText(http.StatusInternalServerError)})
	},
}

DefaultRecoverConfig is the default Recovery config.

Functions

func NewCors

func NewCors() kid.MiddlewareFunc

NewCors returns a new CORS config.

func NewCorsWithConfig

func NewCorsWithConfig(cfg CorsConfig) kid.MiddlewareFunc

NewCorsWithConfig returns a new CORS middleware with the given config.

func NewLogger added in v0.3.0

func NewLogger() kid.MiddlewareFunc

NewLogger returns a new logger middleware.

func NewLoggerWithConfig added in v0.3.0

func NewLoggerWithConfig(cfg LoggerConfig) kid.MiddlewareFunc

NewLoggerWithConfig returns a new logger middleware with the given config.

func NewRecovery

func NewRecovery() kid.MiddlewareFunc

NewRecovery returns a new Recovery middleware.

func NewRecoveryWithConfig

func NewRecoveryWithConfig(cfg RecoveryConfig) kid.MiddlewareFunc

NewRecoveryWithConfig returns a new Recovery middleware with the given config.

Types

type CorsConfig

type CorsConfig struct {
	// AllowedOrigins specifies which origins can access the resource.
	// If "*" is in the list, all origins will be allowed.
	//
	// Defaults to ["*"]
	AllowedOrigins []string

	// AllowOriginFunc is a custom function for validating the origin.
	// The origin will always be set and you don't need to check that in this function.
	//
	// If you set this function the rest of validation logic will be ignored.
	//
	// Defaults to nil.
	AllowOriginFunc func(c *kid.Context, origin string) bool

	// AllowedMethods is the list of allowed HTTP methods.
	//
	// Defaults to ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"].
	AllowedMethods []string

	// AllowedHeaders is the list of the custom headers which are allowed to be sent.
	//
	// If "*" is in the list, all headers will be allowed.
	AllowedHeaders []string

	// ExposedHeaders a list of headers that clients are allowed to access.
	//
	// Defaults to [].
	ExposedHeaders []string

	// MaxAge is the maximum duration that the response to the preflight request can be cached before another call is made.
	// In second percision.
	//
	// Will not be used if 0.
	// Defaults to 0.
	MaxAge time.Duration

	// AllowCredentials if true, cookies will be allowed to be included in cross-site HTTP requests.
	//
	// defaults to false.
	AllowCredentials bool

	// AllowPrivateNetwork if true, allow requests from sites on “public” IP to this server on a “private” IP.
	//
	// defaults to false.
	AllowPrivateNetwork bool
	// contains filtered or unexported fields
}

CorsConfig is the config used to build CORS middleware.

type LoggerConfig added in v0.3.0

type LoggerConfig struct {
	// Logger is the logger instance.
	// Optional. If set, Out, Level and Type configs won't be used.
	Logger *slog.Logger

	// Out is the writer that logs will be written at.
	// Defaults to os.Stdout.
	Out io.Writer

	// Level is the log level used for initializing a logger instance.
	// Defaults to slog.LevelInfo.
	Level slog.Leveler

	// SuccessLevel is the log level when status code < 400.
	// Defaults to slog.LevelInfo.
	SuccessLevel slog.Leveler

	// ClientErrorLevel is the log level when status code is between 400 and 499.
	// Defaults to slog.LevelWarn.
	ClientErrorLevel slog.Leveler

	// ServerErrorLevel is the log level when status code >= 500.
	// Defaults to slog.LevelError.
	ServerErrorLevel slog.Leveler

	// Type is the logger type.
	// Defaults to JSON.
	Type LoggerType

	// Skipper is a function used for skipping middleware execution.
	// Defaults to nil.
	Skipper func(c *kid.Context) bool
}

LoggerConfig is the config used to build logger middleware.

type LoggerType added in v0.3.0

type LoggerType string

LoggerType is the type for specifying logger type.

const (
	// JSONLogger is the JSON logger type.
	TypeJSON LoggerType = "JSON"

	// TextLogger is the text logger type.
	TypeText LoggerType = "TEXT"
)

type RecoveryConfig

type RecoveryConfig struct {
	// LogRecovers logs when a recovery happens, only in debug mode.
	LogRecovers bool

	// PrintStacktrace prints the entire stacktrace if true, only in debug mode.
	PrintStacktrace bool

	// Writer is the writer for logging recoveries and stacktraces.
	Writer io.Writer

	// OnRecovery is the function which will be called when a recovery occurs.
	OnRecovery func(c *kid.Context, err any)
}

RecoveryConfig is the config used to build a Recovery middleware.

Jump to

Keyboard shortcuts

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