Documentation
¶
Index ¶
- Variables
- func NewCors() kid.MiddlewareFunc
- func NewCorsWithConfig(cfg CorsConfig) kid.MiddlewareFunc
- func NewLogger() kid.MiddlewareFunc
- func NewLoggerWithConfig(cfg LoggerConfig) kid.MiddlewareFunc
- func NewRecovery() kid.MiddlewareFunc
- func NewRecoveryWithConfig(cfg RecoveryConfig) kid.MiddlewareFunc
- type CorsConfig
- type LoggerConfig
- type LoggerType
- type RecoveryConfig
Constants ¶
This section is empty.
Variables ¶
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.
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.
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 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.