middleware

package
v1.13.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package middleware holds the Gin middleware the Kargo API server composes its REST router from.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleErrors

func HandleErrors() gin.HandlerFunc

HandleErrors returns Gin middleware that answers the last error a handler or another middleware reported. It is the only thing that writes an error response, so that every one has the same shape.

An error carrying a client-facing status (libhttp.HTTPError, or a Kubernetes status error) is answered with that status and its message. A request body over its limit is answered with a 413. Anything else is, by definition, an error nobody anticipated, and is answered as an internal server error whose body discloses nothing; the request logging middleware records the error itself.

func LimitBody

func LimitBody(maxBytes int64) gin.HandlerFunc

LimitBody returns Gin middleware that limits request body size. It wraps the request body with http.MaxBytesReader which returns an error when the limit is exceeded.

func Recover

func Recover() gin.HandlerFunc

Recover returns Gin middleware that recovers from a panic in any handler downstream of it and reports it as an error rather than answering the request itself. The error-handling middleware then responds as it does to any other unanticipated error.

This exists in preference to gin.Recovery(), which answers a panic with c.AbortWithStatus and therefore sends a 500 with an empty body, leaving a client nothing to parse and bypassing the error-handling middleware altogether. Reporting the panic instead keeps every error response the same shape and keeps response writing in one place.

This must be registered inside the error-handling middleware, so that the error it reports is still there to be found when that middleware inspects the request on its way back out.

func RequireFeature

func RequireFeature(enabled bool, disabledErr error) gin.HandlerFunc

RequireFeature returns Gin middleware that refuses every request with the given error when the feature it guards is not enabled, so that handlers for optional features need not check for themselves. The error should carry the status the client is to see; the API server's error-handling middleware reports an error without one as an internal server error.

func RequireProject

func RequireProject(cl client.Client) gin.HandlerFunc

RequireProject returns Gin middleware that confirms the Project named by the request's "project" path parameter exists, looking it up with the given client, before any handler runs. A request for anything in a Project that does not exist is answered with the lookup's error, a 404, and no handler has to check for itself.

Types

type ErrorResponse

type ErrorResponse struct {
	Error string `json:"error"`
}

ErrorResponse is the body of every error response the REST API sends.

Jump to

Keyboard shortcuts

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